【发布时间】:2016-09-04 16:34:46
【问题描述】:
我对 Rails 和编写我的第一个应用程序还很陌生。只是无法弄清楚如何在我的控制器中定位以下内容。
控制器
def index
@tool = Tool.find(params[:id])
@favorites = current_user.favorites
@tools = Tool.where(user_id: current_user).order("created_at DESC")
@user = current_user
end
索引视图
%h2 My Favorite Tools
- @favorites.each do |tool|
= image_tag tool.cover_filename.url
%h2= link_to tool.title, tool
%p= tool.subtitle
%p= @tool.impressionist_count
%p= link_to @tool.get_upvotes.size, like_tool_path(@tool), method: :get
%p= link_to "Edit", edit_tool_path(tool)
%p
http://ocubit.com/tools/
= @tool.id
%p= time_ago_in_words(tool.created_at)
%h2 My Tools
- @tools.each do |tool|
= image_tag tool.cover_filename.url
%h2= link_to tool.title, tool
%p= tool.subtitle
%p= @tool.impressionist_count
%p= link_to "Edit", edit_tool_path(tool)
%p
http://ocubit.com/tools/
= @tool.id
%p= time_ago_in_words(tool.created_at)
= link_to "View Your Profile", '/users/'+@user.id.to_s
-if @user.use_gravatar?
= image_tag gravatar_for @user
- else
= image_tag @user.avatar_filename.url
%h1= @user.username
= link_to "Edit", edit_user_registration_path
如果我在浏览器中运行它会出现以下错误:
ActiveRecord::RecordNotFound in ToolsController#index
Couldn't find Tool with 'id'=
我已经将控制器更改为(测试)
@tool = Tool.find(1)
这行得通,所以问题必须存在。我就是想不通。
提前感谢您的帮助!
【问题讨论】:
-
将
id参数传递给index操作有点不寻常。通常index操作的路由不会为您正在查看的集合使用id参数。我认为您可能希望完全删除@tool = Tool.find(params[:id])行,然后将视图中的所有@tool更改为tool。 -
检查您的 rake 路线。还要检查错误页面上的参数部分以确保设置了 :id 参数。
标签: ruby-on-rails ruby ruby-on-rails-4 activerecord controller