AlbumsController#edit中的ActiveRecord :: RecordNotFound

时间:2016-06-27 18:58:27

标签: ruby-on-rails ruby activerecord

虽然在show动作中我正试图链接到专辑控制器的编辑动作,但我不断收到ActiveRecord错误说:找不到带有'id'的相册=:id [WHERE“专辑”。“user_id” =?]

相册控制器:

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

查看:

    <% 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 %>

1 个答案:

答案 0 :(得分:0)

您需要为edit_album_path指定编辑路径@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 %>

假设您已在routes.rb中定义了专辑编辑路径的路径,或者您可以按照以下方式在routes.rb中设置专辑资源,这将创建您构建CRUD操作所需的路径:

resources :albums 

您可以使用应用程序路径中的rake routes | grep album命令检查路由。