【问题标题】:Rails 4 Error: ActiveRecord::RecordNotFound / Couldn't find 'id'=Rails 4 错误:ActiveRecord::RecordNotFound / 找不到 'id'=
【发布时间】: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


【解决方案1】:

你实际上不需要这一行:

@tool = Tool.find(params[:id])

如果您在索引视图中,您希望显示所有工具,而不仅仅是一个。那将是 :show 视图。您在索引视图中没有 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

【讨论】:

    【解决方案2】:

    这是由于params[:id] 等于nil。尝试打开此 URL,然后将代码更改为:

    # open this URL to pass parameter tool_id
    http://localhost:3000/favorites?tool_id=1
    
    # in view 
    <%= link_to "Favorites", favorites_path(tool_id: 1) %>
    
    # controller
    def index
      @tool = Tool.find(params[:tool_id])
    end
    

    【讨论】:

      【解决方案3】:

      关键是您的索引控制器中没有 params[:id]。删除这一行: @tool = Tool.find(params[:id])

      在您的视图中只使用“工具”而不是 @tool

      【讨论】:

      • 提前致谢!
      【解决方案4】:

      在这之前已经有很多正确的答案了,但我想做更多的事情然后给你解决方案。

      其实是什么 @tool = Tool.find(params[:id]) 确实是,它将从您的 URL 中获取 ID..

      假设你的浏览器上有

      http://localhost:3000/tool/1
      

      这会让你的 ID 参数成为第一名。

      但这里很棒的是,你在你的索引上......每次你去你的索引“网站”(准确地说是索引视图),你会在你的 URL 上看到类似的东西

      ttp://localhost:3000/tools
      

      这就是为什么您收到此错误“URL 上没有 ID”

      但是 ruby​​ on rails 就像任何其他语言一样,您需要理解它,而 DAMM ruby​​ 就是在这方面的意思......对于 ruby​​..Tools != Tool 这个小 's' 意味着您想查看所有工具,因此无需发送 ID也就是说,您将看到 ID = 1 的工具

      事实上,您希望在 Controller 索引上显示您所有的工具,所以......

      @tools = Tool.all

      或者和你current_user和收藏列表有关系的东西

      【讨论】:

      • 非常感谢您的解释!明白了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多