nums nums - 5 months ago 12
Ruby Question

ActiveRecord::RecordNotFound in AlbumsController#edit

While inside the show action I'm trying to link to the album controller's edit action but I keep getting an ActiveRecord Error saying: Couldn't find Album with 'id'=:id [WHERE "albums"."user_id" = ?]

Album controller:

def edit
@album = current_user.albums.find(params[:id])
end

def update
@album = current_user.albums.find(params[:id])
if @album.update(album_params)
redirect_to '/albums/:id'
else
render '/albums/edit'
end
end

def show
@album = current_user.albums.find(params[:id])
@photos = @album.photos
@interests = current_user.interests
end


View:

<% if @album.description.nil? %>
<div class="description" id="new"><%= link_to 'Add Description', "/albums/:id/edit", :style => "text-decoration:none;" %></div>
<% else %>
<div class="description"><%= @album.description %></div>
<% end %>

dkp dkp
Answer

You need to specify the edit path edit_album_path for the @album:

<% if @album.description.nil? %>
    <div class="description" id="new"><%= link_to 'Add Description', edit_album_path(@album), :style => "text-decoration:none;" %></div>
<% else %>
    <div class="description"><%= @album.description %></div>
<% end %>

Assuming you have defined the route for album edit path in routes.rb or you can simply have the album resource in routes.rb as follows which would create the paths that are needed for you to build the CRUD actions:

resources :albums 

You can check your routes using the command rake routes | grep album in the your application path.