我对 Rails 和编写我的第一个应用程序还很陌生。只是不知道如何在我的 Controller 中定位以下内容。
Controller
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
索引 View
%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'=
我已经将我的 Controller 更改为(测试)
@tool = Tool.find(1)
这行得通,所以问题一定存在。我根本想不通。
预先感谢您的帮助!
最佳答案
你实际上不需要这一行:
@tool = Tool.find(参数[:id])
如果您在索引 View 中,您希望显示所有工具,而不仅仅是一个。这将用于 :show View 。您在索引 View 中没有 params[:id],您没有在 url 中指定它。
所以:
def index
@user = current_user
@favorites = @user.favorites
@tools = @user.tools.order("created_at DESC")
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
关于ruby-on-rails - Rails 4 错误:ActiveRecord::RecordNotFound/找不到 'id' =,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39319144/