J. Hübotter J. Hübotter - 3 months ago 8
Ruby Question

Rails 4 Error: ActiveRecord::RecordNotFound / Couldn't find 'id'=



I'm fairly new to rails and into coding my first app. Just can't figure out how to target the following stuff in my 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


Index 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


If I run this in my browser it appears following error:

ActiveRecord::RecordNotFound in ToolsController#index
Couldn't find Tool with 'id'=



I've already changed my controller to (to test)

@tool = Tool.find(1)


And that works, so the issue has to be there. I simply can't figure it out.


Thanks in advance for your help!

Answer

You actually don't need this line:

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

If you are in the index views, you want to show all the tools, not just one. THat would be for the :show view. You don't have a params[:id] in the index view, you are not specifing that in the url.

So:

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
Comments