I am beginner to ROR and I am following this tutorial http://guides.rubyonrails.org/getting_started.html.
So according to this tutorial I want to delete one post. But its not working it showing this error The action 'destroy' could not be found for PostsController
My post controller delete method looks like
def destroy
#post = Post.find(params[:id])
logger.debug "***********************: #{#post.id}"
#post.destroy
redirect_to posts_path
end
In routes I mentioned resource resources :posts but still it is giving error for destroy action. Am I doing something wrong. Need Help.
Did you mention the method as delete in your views?
If you are using Rails 4, you should do:
<%=link_to 'Destroy', post_path(post), method: :delete, data: { confirm: 'Are you sure?' } %>
In Rails 3:
<%=link_to 'Destroy', post_path(post), method: :delete, confirm: 'Are you sure?' %>
Related
I"m using Rails 4.2.3 and I’m getting a “No route matches {:action=>"delete", :controller=>"my_objects", :id=>8}” error when I visit my view which contains the below link
<%= link_to 'Delete',url_for(controller: 'my_objects', action: :delete,id: my_object_time.my_object.id),method: :delete, data: {confirm: "Are you sure you want to delete this data?"} %>
However, in my routes/config.rb file, I have included
resources :my_objects
I even have the method in my my_objects_controller.rb file …
def delete
my_object.find(params[:id]).destroy
format.html { redirect_to controller: "users", action: "index", notice: 'Delete successful.' }
end
So I’m not sure why its all falling apart. Any help is appreciated, - Dave
try this:
<%= link_to 'Delete',url_for(controller: 'my_objects', action: :destroy,id: my_object_time.my_object.id),method: :delete, data: {confirm: "Are you sure you want to delete this data?"} %>
or
<%= link_to 'Delete', my_object_time.my_object.id, method: :delete, data: { confirm: 'Are you sure?' } %>
I am trying to delete a particular "work" from an array of "works" which is embedded in user.
In my work.html.erb file in views:-
<% #works.each do |f| %>
<%= link_to 'Destroy', profiles_destroy_path(f), data: {:confirm => 'Are you sure?'}, :method => :delete %>
<% end %>
And in my controller:-
def destroy
#work = current_user.works.find(params[:id])
#work.destroy
respond_to do |format|
format.html { redirect_to root_url }
end
end
I am getting following error:-
Mongoid::Errors::InvalidFind at /profiles/destroy.56fa4d2f498b5908a002e2e8
P.S. - I am new to rails.
Please check params[:id] is not nil. There's no :id at session start and you get the Mongoid::Errors::InvalidFind exception above.
delete action doesn't have path by default..
<%= link_to 'Destroy', profiles_path(f), data: {confirm: 'Are you sure?'}, method: :delete %>
That will call the destroy action from your controller.
And make sure params[:id] is not nil.
PS: use rails 4 annotation (avoid using rockets (=>))
There may be an error in Juan's answer.
I believe you want:
<%= link_to 'Destroy', profile_path(f), method: :delete, data: {confirm: 'Are you sure?'} %>
Note: the singular "profile_path" instead of "profiles_path"
This is because the destroy action uses the same path as the show action, but uses a DELETE method instead of a GET. (Which you can observe by running "rake routes" in your terminal and comparing the two routes.)
actually i use the following code in my _user.html.erb:
<%= link_to "Delete User", user, method: :delete, data: { confirm: "Are you sure?" } %>
For every user i get a link to delete him.
In my controller the delete-method calls the destroy-method:
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted."
redirect_to users_url
end
This all works fine.
What i want now is another link where a call a custom method.
How can i call a custom method?
What i already tried was to add a method with the name test and then i called method: :test but this doesn't work:
No route matches [POST] "/users/2"
Thanks for any idea
Question heading is wrong, pagination means something else.
First configure your routes like
resources :controller_name do
member do
delete 'test'
end
end
Your route will be set ..
test_controller_name DELETE /controller_name/:id/test(.:format) controller_name#test
Call the action from link_to and pass the object id .
<%= link_to 'Destroy', test_controller_path(:id => #object.id), method: :delete, data: { confirm: 'Are you sure?' } %>
I'm doing a Rails blog tutorial and don't fully understand the following link_to code
<%= link_to 'Destroy Comment', [comment.post, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
Why do I have to use:
[comment.post, comment]
and why can't I just write:
#post.comment
My second, related, question is that since I created the "destroy" action in the controller as follows:
def destroy
#post = Post.find(params[:post_id])
#comment = #post.comments.find(params[:id])
#comment.destroy
redirect_to post_path(#post)
end
Why don't I have to mention "destroy" in the link_to code?
<%= link_to 'Destroy Comment', [comment.post, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
The reason why you have to supply both the Post object and the Comment to the link_to helper is because Comment is a nested resource in Post, and both IDs must be known in order to construct the URL. It's actually equivalent to:
link_to 'Destroy Comment', post_comment_path(comment.post, comment), ...
What it's doing is it's resolving the path helper for you, using url_for. See http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects .
You don't have to mention destroy in your link_to because destroy is the name of the action. Your routes file outlines which controllers and actions are associated with which routes.
I assume that you're using resourceful routing, which is shorthand way of defining routes for all of the CRUD actions. See http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions for the mapping between HTTP verb and controller action. You'll see that delete is mapped to destroy, and you're using method: :delete on your link_to.
So there are a lot of things going on here.
1) My guess is that the link_to in the first part is within a loop. Is that true? That would be something like #post.comments.each do |comment|. If that's the case, then likely what's happening is you have comments nested under posts. That documentation can be found here. The brackets are to identify the comment, which you need a post id for. You could probably also do [#post, comment], which would work just as well. You can't just write #post.comment because it's not enough information to identify the correct comment.
2) Rails takes HTTP verbs to identify which action to call from the controller. You're sending an HTTP DELETE request to /posts/:post_id/comments/:id, which the routes file then figures out belongs to the comments controller. That documentation can be found here and here.
I have a List object, with nested Tasks. I have created a page that displays individual tasks, and also a page that allows a user to edit individual tasks. I now want to add the ability to delete a task from a list on the tasks edit page. Using the following code
<%= link_to 'Delete this task',#task, confirm: 'Are you sure?', method: :delete %>
yields
undefined task_path method
This code is on the show.html.erb page, where I call #task to display all of the data stored within the task, so I believe that this issue may be a routing error of some kind, however I cannot seem to figure it out.
The related controller method is
def destroy
#task = Task.find(params[:id])
#task.destroy
respond_to do |format|
format.html { redirect_to list_tasks_path(#task) }
format.json { head :ok }
end
end
I thought that with the delete method the #task I supplied would just be sent to the destroy method via params, but this error seems to be showing that this isn't exactly how it works. So how can I properly destroy a nested resource in Rails?
edit:
Here is the route file with nested resources:
MyApp::Application.routes.draw do
resources :lists do
resources :tasks
end
get "home/index"
root :to => 'home#index'
end
Thank you for your help!
You should have #list setup, or use #task.list (assuming you have a belong to relationship), and you could do the following:
<%= link_to "Delete this task", list_task_path(#task.list, #task), confirm: "Are you sure?", method: :delete %>
Cheers!
Try this:
<%= link_to 'Delete this task', list_task_path(#list, #task), confirm: 'Are you sure?', method: :delete %>
Or if you want it more compact (like you've written it):
<%= link_to 'Delete this task', [#list, #task], confirm: 'Are you sure?', method: :delete %>
Either way, since it's a nested resource, you must pass in both the #list and #task objects.