简体   繁体   中英

Couldn't find with 'id'

when i use method 'destroy' show me an error

Couldn't find Post with 'id'=#<Post::ActiveRecord_Relation:0x000000045e14c0>

method destroy

def destroy
    @post = Post.find(params[:id])
    @post.destroy

    redirect_to posts_path
end

view

<%= link_to 'Destroy', post_path(@posts),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>

sorry for my bad English

You have @posts variable defined as a ActiveRecord::Relation (some kind of models array, defined with @posts = Post.all or anything like that). To fix your issue, if you have link_to call inside index url, most probably you are doing something like:

<% @posts.each do |post| %>
  # link_to call somewhere here
<% end %>

Then you have to change your link_to call to

<%= link_to 'Destroy', post_path(post),
          method: :delete,
          data: { confirm: 'Are you sure?' } %>

Notice we use variable defined in .each do |var| block, here it's post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM