AlbumsController#show中的ActiveRecord :: RecordNotFound

时间:2016-06-21 20:35:15

标签: ruby-on-rails ruby activerecord

在我的网络应用中,我试图制作一个节目动作,在该动作中,当点击该相册时,会显示特定相册的current_users照片。但是,每次点击显示操作的链接时,我都会收到一条错误消息,说明“无法找到相册”,并且“#id; =:id [WHERE"相册&#34 ;" USER_ID" =?]'。 show动作如下所示:

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

观点:

<div class="main">
<div class="main-background">

    <% @albums.each do |x| %>
            <div class="big-albums"><%= link_to x.name, "/albums/:id", :style => "text-decoration:none;" %></div>
    <% end %>

    <div class="new-album">
        <%= link_to 'New Album', "/albums/new", :style => "text-decoration:none;" %>
        <div class="plus-sign">
            <%= link_to '+', "/albums/new", :style => "text-decoration:none; color:black; font-size:80px; color:#85adad;" %>
        </div>          
    </div>

</div>

路线:

      get '/albums' => 'albums#index', as: :albums
  get '/albums/new' => 'albums#new'
  post '/albums/new' => 'albums#create'
  get '/albums/:id' => 'albums#show'
  get '/albums/:id/destroy' => 'albums#destroy'

1 个答案:

答案 0 :(得分:0)

您的问题是您正在显示的link_to网址

link_to x.name, "/albums/:id"

那是将:id指定为传递给URL的参数。相反,您希望执行"/albums/#{x.id}"之类的操作,以便它将相册ID用作URL路径上的ID参数。