Home>

I want to delete only the record with id2 in the DB

id user_id page_id
1 1 1
2 1 2
Corresponding source code

Delete button

<% = link_to "Delete article", "/ posts/# {@ my_post.id}", method:: delete, data: {confirm:'Are you sure I want to delete? '}, class: "delete-btn"%>
def destroy
  @my_post = Post.find_by (user_id: current_user.id, page_id: params [: id])
  @ my_post.destroy
end
@my_post = Post.find_by (user_id: current_user.id, page_id: params [: id])


Is nil

What I tried

Check the contents with binding.pry
page_id was nil
url is

http: // localhost: 3000/pages/2


Because it is, I thought that 2 could be obtained with params [: id], but it seems different
I tried to find out if there is a way to send id2 with link_to
I saw that Prefix can be sent as an argument, but I am worried because Prefix can not do it.

<% = link_to "Delete article", "/ posts/# {@ my_post.id}", method:: delete, data: {confirm:'Are you sure I want to delete? '}, class: "delete-btn"%>
Solution
<% = link_to "Delete article", "/ posts/# {@ my_post.post_id}", method:: delete, data: {confirm:'Are you sure I want to delete? '}, class: "delete-btn"%>