Rails 5 嵌套资源 URL 与 has-many 通过关联



我利用 Has-Many 通过关联方法创建了一个小型测试应用程序,但我似乎可以在我的嵌套路由中使用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]

位置.rb

class Location < ApplicationRecord
  belongs_to :product
  belongs_to :category
end

类别.rb

class Category < ApplicationRecord
  has_many :locations
  has_many :products, through: :locations
end

产品.rb

class Product < ApplicationRecord
  has_many :locations
  has_many :categories, through: :locations
end

路线.rb

Rails.application.routes.draw do
  resources :locations do
    resources :categories do
      resources :products
    end
  end
  root :to => 'locations#index'
end

categories/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/轨道/信息/路线

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

对于路线:

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

您需要几个东西,位置 id 对象和类别 id 对象:

<!-- /locations/:location_id/categories/:id -->
<%= link_to 'Show', [{location_id}, {category_id}] %>
location_category_path  GET /locations/:location_id/categories/:id(.:format)    

正如你在这里看到的,路线:location_category_path需要两样东西::location_id:id(你想要引用的类别id)。

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

还有

一种更短的方法来编写此 URL:[location_id, category_id] 。只需编写一个两个 id 都以 location_id 开头的数组。

只是为了补充阿尔斯兰的答案。

如果尚未设置控制器,

则可能需要在控制器中设置@location

app/controllers/categories_controller.rb

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

app/views/categories/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 %>

对于路由 URL 参数,您可以使用以下任一方法:

location_category_path(@location.id, category.id)

当您执行bundle exec rake routes时,它遵循您的路线顺序(即:location_id然后:id),或者您也可以像下面这样具体:

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

相关内容

  • 没有找到相关文章

最新更新