Rails 5嵌套资源具有多个通过关联的URL

时间:2017-11-01 19:11:53

标签: ruby-on-rails ruby ruby-on-rails-5

我已经使用Has-Many Through Association方法创建了一个小型测试应用程序,但我似乎可以在嵌套路由中使用link_to

错误消息

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

3 个答案:

答案 0 :(得分:1)

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

正如您在此处所见,路线:location_category_path需要两件事::location_id&amp; :id(您要引用的id类别。)

在您的情况下,您只指定一个,而不指定另一个。您还需要指定:location_id

还有一种更短的方式来编写此网址:[location_id, category_id]。只需编写一个包含两个以location_id开头的ID的数组。

答案 1 :(得分:1)

路线:

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

您需要几件事,即位置ID对象和类别ID对象:

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

答案 2 :(得分:-1)

只是为了加入Arslan的答案。

如果你还没有,你可能需要在你的控制器中设置@location

应用/控制器/ categories_controller.rb

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

应用/视图/类别/ index.html.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_category_path(@location.id, category.id)

当您执行:location_id时,会按照您的路线顺序(:id然后bundle exec rake routes),或者您也可以具体如下:

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