Home>
I want to delete only the record with id2 in the DB
id | user_id | page_id |
---|---|---|
1 | 1 | 1 |
2 | 1 | 2 |
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
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"%>
-
Answer # 1
Related articles
- ruby on rails 6 - the delete function cannot be implemented well
- ruby on rails - comment posting function actioncontroller :: urlgenerationerror
- ruby on rails - about matters that cannot be migrated
- ruby on rails - about request spec of rspec about test code at login
- [ruby on rails] about header setting using bootstrap
- ruby - [rails] implementation of search function
- ruby on rails - about the description in applicationjs
- ruby on rails - about the argument of rubyonrails params
- ruby - [rails] the cart function cannot get the cart id associated with the user
- ruby - about routing using rails collection
- ruby on rails - about image distribution on heroku and s3
- ruby on rails - implementation of validation function
- ruby on rails - [rails] about the problem that screen transition is not possible (error does not occur)
- ruby on rails - rails render: about the matter that the url after new is different
- ruby on rails - about image posting page argumenterror
- ruby on rails - about rails composite indexes
- ruby on rails - about saving comments
- ruby - about rails routing
- ruby on rails - about deploying apps using s3 function
Related questions
- ruby - passing variables using render partial
- ruby - deployment error on heroku
- ruby - output the total integer value held by each user
- ruby - i want to use the search method for active hash data
- ruby - destroy action does not respond
- ruby - how do you see the rails controller relative path? (carrierwave directory deleted)
- ruby - when deploying on capistrano, i get the error "access denied for user'ec2-user' @'localhost' (using password: no)&qu
- javascript - run multiple times with js + each do
- how to reflect the layout downloaded from the external site in ruby on rails?
- ruby - how nice! function routes do not pass well (relation) [rails]
I feel that the id (object in the variable) used for the variable of the exchange of the template and the method and the id of the column processed by DB are mixed up.
So, I think it's better to match the id value sent when executing the method to the template side.