Has-Many Through Associationメソッドを使用して小さなテストアプリケーションを作成しましたが、ネストされたルート内でlink_to
を使用するように見えます。Rails 5ネストされたリソースHas-Many Throughアソシエーションを使用したURL
エラーメッセージ
No route matches {:action=>"show", :controller=>"categories", :location_id=>#<Category id: 1, name: "parties", created_at: "2017-11-01 17:40:25", updated_at: "2017-11-01 17:40:25">}, missing required keys: [:id]
location.rb
class Location < ApplicationRecord
belongs_to :product
belongs_to :category
end
category.rb
class Category < ApplicationRecord
has_many :locations
has_many :products, through: :locations
end
product.rb
class Product < ApplicationRecord
has_many :locations
has_many :categories, through: :locations
end
routes.rbを
Rails.application.routes.draw do
resources :locations do
resources :categories do
resources :products
end
end
root :to => 'locations#index'
end
カテゴリ/ index.htmlを
...
<% @categories.each do |category| %>
<tr>
<td><%= category.name %></td>
<td><%= link_to 'Show', location_category_path(category) %></td>
<td><%= link_to 'Edit', edit_location_category_path(category) %></td>
<td><%= link_to 'Destroy', location_categories_path(category), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
...
127.0.0.1:3000/rails/info/routes
location_category_products_path GET /locations/:location_id/categories/:category_id/products(.:format)
products#index
POST /locations/:location_id/categories/:category_id/products(.:format)
products#create
new_location_category_product_path GET /locations/:location_id/categories/:category_id/products/new(.:format)
products#new
edit_location_category_product_path GET /locations/:location_id/categories/:category_id/products/:id/edit(.:format)
products#edit
location_category_product_path GET /locations/:location_id/categories/:category_id/products/:id(.:format)
products#show
PATCH /locations/:location_id/categories/:category_id/products/:id(.:format)
products#update
PUT /locations/:location_id/categories/:category_id/products/:id(.:format)
products#update
DELETE /locations/:location_id/categories/:category_id/products/:id(.:format)
products#destroy
location_categories_path GET /locations/:location_id/categories(.:format)
categories#index
POST /locations/:location_id/categories(.:format)
categories#create
new_location_category_path GET /locations/:location_id/categories/new(.:format)
categories#new
edit_location_category_path GET /locations/:location_id/categories/:id/edit(.:format)
categories#edit
location_category_path GET /locations/:location_id/categories/:id(.:format)
categories#show
PATCH /locations/:location_id/categories/:id(.:format)
categories#update
PUT /locations/:location_id/categories/:id(.:format)
categories#update
DELETE /locations/:location_id/categories/:id(.:format)
categories#destroy
locations_path GET /locations(.:format)
locations#index
POST /locations(.:format)
locations#create
new_location_path GET /locations/new(.:format)
locations#new
edit_location_path GET /locations/:id/edit(.:format)
locations#edit
location_path GET /locations/:id(.:format)
locations#show
PATCH /locations/:id(.:format)
locations#update
PUT /locations/:id(.:format)
locations#update
DELETE /locations/:id(.:format)
locations#destroy
root_path GET/
locations#index
ほとんどの場合、その特定のカテゴリを参照する必要がある2つのIDを追加します: '<%= lin k_to '表示'、[location_id、category_id]%> '希望が助けてくれる! – Cyzanfar