I am trying to create an info page for each restaurant in a Ruby on rails app. I created an action info
in Restaurant controller
and added a line in routes.rb
. but it gives an error
我正在尝试在Ruby on rails应用程序中为每个餐厅创建一个信息页面。我在餐厅控制器中创建了一个动作信息,并在routes.rb中添加了一行。但它给出了一个错误
"ActiveRecord::RecordNotFound in RestaurantsController#info" Couldn't find Restaurant with 'id'=..
“在餐馆控制器#info中的ActiveRecord :: RecordNotFound”找不到带有'id'的餐馆= ..
What is wrong with my code?
我的代码出了什么问题?
This is my Restaurant controller:
这是我的餐厅控制器:
class RestaurantsController < ApplicationController
before_action :set_restaurant, only: [:show, :edit, :update, :destroy, :info]
def info
end
private
def set_restaurant
@restaurant = Restaurant.find(params[:id])
end
end
This is my routes.rb:
这是我的routes.rb:
Rails.application.routes.draw do
resources :restaurants do
get '/info', to: 'restaurants#info'
end
end
And this a link to restaurant info page:
这是餐厅信息页面的链接:
<%= link_to 'Info', restaurant_info_path(@restaurant) %>
2 个解决方案
#1
1
ActiveRecord::RecordNotFound in RestaurantsController#info
RestaurantsController中的ActiveRecord :: RecordNotFound #info
By having the custom route nested inside the resources :restaurants
, generates the route with wrong keys. When you run rake routes
, you can see this route
通过将自定义路由嵌套在资源:restaurants中,生成具有错误键的路由。当您运行rake路线时,您可以看到此路线
restaurant_info GET /restaurants/:restaurant_id/info(.:format) restaurants#info
So this route has :restaurant_id
not :id
. So you cannot do params[:id]
to fetch the value here in this line @restaurant = Restaurant.find(params[:id])
which fails with that error. Changing params[:id]
to params[:restaurant_id]
should solve your problem but that is not the right solution.
所以这条路线有:restaurant_id not:id。所以你不能用params [:id]来获取这行@restaurant = Restaurant.find(params [:id])中的值,该值因错误而失败。将params [:id]更改为params [:restaurant_id]应该可以解决您的问题,但这不是正确的解决方案。
The obvious and the ideal solution would be to tweak your custom route, so it can generated like this
显而易见且理想的解决方案是调整自定义路由,因此它可以像这样生成
restaurant_info GET /restaurants/:id/info(.:format) restaurants#info
You can tweak your custom route to below to achieve this and not having that nested inside resources :restaurants
您可以将自定义路由调整到下面以实现此目的,而不是嵌套内部资源:餐馆
get 'restaurants/:id/info', to: 'restaurants#info', as: :restaurant_info
resources :restaurants
#2
0
This is because the set_restaurant
method isn't getting any params, check that and come back if it is receiving them correctly.
这是因为set_restaurant方法没有得到任何参数,检查并返回它是否正确接收它们。
#1
1
ActiveRecord::RecordNotFound in RestaurantsController#info
RestaurantsController中的ActiveRecord :: RecordNotFound #info
By having the custom route nested inside the resources :restaurants
, generates the route with wrong keys. When you run rake routes
, you can see this route
通过将自定义路由嵌套在资源:restaurants中,生成具有错误键的路由。当您运行rake路线时,您可以看到此路线
restaurant_info GET /restaurants/:restaurant_id/info(.:format) restaurants#info
So this route has :restaurant_id
not :id
. So you cannot do params[:id]
to fetch the value here in this line @restaurant = Restaurant.find(params[:id])
which fails with that error. Changing params[:id]
to params[:restaurant_id]
should solve your problem but that is not the right solution.
所以这条路线有:restaurant_id not:id。所以你不能用params [:id]来获取这行@restaurant = Restaurant.find(params [:id])中的值,该值因错误而失败。将params [:id]更改为params [:restaurant_id]应该可以解决您的问题,但这不是正确的解决方案。
The obvious and the ideal solution would be to tweak your custom route, so it can generated like this
显而易见且理想的解决方案是调整自定义路由,因此它可以像这样生成
restaurant_info GET /restaurants/:id/info(.:format) restaurants#info
You can tweak your custom route to below to achieve this and not having that nested inside resources :restaurants
您可以将自定义路由调整到下面以实现此目的,而不是嵌套内部资源:餐馆
get 'restaurants/:id/info', to: 'restaurants#info', as: :restaurant_info
resources :restaurants
#2
0
This is because the set_restaurant
method isn't getting any params, check that and come back if it is receiving them correctly.
这是因为set_restaurant方法没有得到任何参数,检查并返回它是否正确接收它们。