Ruby on rails Rails 5嵌套资源URL具有多个直通关联
我已经创建了一个小的测试应用程序,利用Has-Many-Through-Association方法,但我似乎可以在嵌套路由中使用link_ 错误消息 categories/index.html 正如您在这里看到的,route:location\u category\u path需要两件事::location\u id&:id您想要引用的类别的id 在您的情况下,您只指定一个,而不是另一个。您还需要指定:location\u id 还有一种较短的方法可以编写此URL:[位置\标识,类别\标识]。只需编写一个数组,其中两个id都以location_id开头。用于路由:Ruby on rails Rails 5嵌套资源URL具有多个直通关联,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我已经创建了一个小的测试应用程序,利用Has-Many-Through-Association方法,但我似乎可以在嵌套路由中使用link_ 错误消息 categories/index.html 正如您在这里看到的,route:location\u category\u path需要两件事::location\u id&:id您想要引用的类别的id 在您的情况下,您只指定一个,而不是另一个。您还需要指定:location\u id 还有一种较短的方法可以编写此URL:[位置\标识,类别\标识]。只
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 app/controllers/categories_controller.rb 它遵循路由的顺序,即:location\u id然后:id,当您绑定exec-rake路由时,或者您也可以像下面这样指定:
location_category_path(location_id: @location.id, id: category.id)
您就快到了,只需添加两个ID,您将需要引用该特定类别:。希望有帮助!你的假设是错误的,请阅读问题有很多:地点。因此,您的解决方案不起作用。实际上,您会得到一个错误:未定义的方法id,无需担心:快乐编码!我如上所述添加了数组,并收到了错误未定义的局部变量或方法location\u id'..`然后我将链接格式化为'location\u id和category\u id'的“并收到了错误未定义的方法location”。我想说的是,您需要在数组中实际添加位置Id和类别Id。你明白我说的吗?
class Product < ApplicationRecord
has_many :locations
has_many :categories, through: :locations
end
Rails.application.routes.draw do
resources :locations do
resources :categories do
resources :products
end
end
root :to => 'locations#index'
end
...
<% @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 %>
...
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)
location_category_path GET /locations/:location_id/categories/:id(.:format)
<!-- /locations/:location_id/categories/:id -->
<%= link_to 'Show', [{location_id}, {category_id}] %>
class CategoriesController < ApplicationController
def index
@location = # your code here
end
end
<% @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_category_path(location_id: @location.id, id: category.id)