2017-11-01 6 views
0

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 
+0

ほとんどの場合、その特定のカテゴリを参照する必要がある2つのIDを追加します: '<%= lin k_to '表示'、[location_id、category_id]%> '希望が助けてくれる! – Cyzanfar

答えて

1

:ちょうどアルスラーン」の答えに追加する

<!-- /locations/:location_id/categories/:id --> 
<%= link_to 'Show', [{location_id}, {category_id}] %> 
+0

上記のように配列を追加し、 '未定義のローカル変数またはメソッド' location_id '...を受け取りました。 ''%' = link_to 'Show'、location_category_path(location_id:category.location.id、id :category.category_id)%> 'を返し、' のための未定義のメソッド 'location 'を受け取りました。 –

+0

Location_idとcategory_idは単にここのプレースホルダです。私が言いたいのは、配列に位置IDとカテゴリIDを実際に追加する必要があるということです。あなたは私が言っているものを手に入れますか? – Cyzanfar

1
location_category_path GET /locations/:location_id/categories/:id(.:format)  

あなたがここに見ることができるように、ルート:location_category_path必要な2つのこと::location_id & :id(あなたが参照したいカテゴリのid)。

あなたのケースでは、あなたは1つしか指定しておらず、他のユーザーは指定していません。 :location_idも指定する必要があります。

[location_id, category_id]という短いURLがあります。 location_idで始まる両方のIDを持つ配列を書くだけです。

ルートの
-1

:あなたはいくつかのこと、ロケーションIDオブジェクトとカテゴリIDのオブジェクトが必要

location_category_path GET /locations/:location_id/categories/:id(.:format)  

まだコントローラがない場合は、コントローラに@locationを設定する必要があります。

アプリ/コントローラ/ categories_controller.rb

class CategoriesController < ApplicationController 
    def index 
    @location = # your code here 
    end 
end 

アプリ/ビュー/カテゴリ/ index.htmlを。ルートのURLパラメータのERB

<% @categories.each do |category| %> 
    <tr> 
    <td><%= category.name %></td> 
    <td><%= link_to 'Show', location_category_path(@location.id, category.id) %></td> 
    <td><%= link_to 'Edit', edit_location_category_path(@location.id, category.id) %></td> 
    <td><%= link_to 'Destroy', location_categories_path(@location.id, category.id), method: :delete, data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
<% end %> 

、いずれかを使用でき:あなたのルートの順序(:location_id、その後:idである)あなたがbundle exec rake routesを行うとき、またはあなたがすることもできるが、以下の

location_category_path(@location.id, category.id) 

location_category_path(location_id: @location.id, id: category.id) 
+0

あなたの前提が間違っています。質問を読んでください( 'has_many:locations')。したがって、あなたのソリューションはうまくいかないでしょう。実際にはエラーが発生します: '' ActiveRecord :: Associations :: CollectionProxy []>の定義されていないメソッドID – Cyzanfar

+0

@Cyzanfarそうですよ!私の悪い。 –

+1

心配なし:)幸せなコーディング! – Cyzanfar

関連する問題