當我單擊我的 link_to 時,它需要 http://localhost:3000/categories/id 而不是 http://localhost:3000/categories/1。當我將 1 放入 URL 時,它會將我帶到正確的頁面,但我希望它通過鏈接作業,所以我懷疑這是一個路由問題。
這些是我的路線
resources :categories, only: [ :index, :show ] do
resources :stocks, only: [ :index, :show ]
end
resources :stocks, only: [ :index, :show ] do
resources :histories, only: [ :index ]
end
我的鏈接
<% @categories.each do |c| %>
<%= link_to category_path(:id) do %>
<%= c.name %>
<% end %>
這是我的類別控制器
def index
@categories = Category.all
@stocks = Stock.all
end
def show
@category = Category.find(params[:id])
@stock = Stock.find(params[:id])
@categories = Category.where(id: @stocks)
@stocks = Stock.where(category_id: @stock.id)
end
任何幫助將不勝感激,謝謝。
uj5u.com熱心網友回復:
您需要將 id 傳遞給路由,這是它的可變部分。從改變
<%= link_to category_path(:id) do %>
至
<%= link_to category_path(c.id) do %>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/510031.html
上一篇:陣列只包含一個特定的字串物件
