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.)
Related
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?' %>
In an index view (a list of attachments for a task), I have a link to destroy (delete) a record (attachment). It automatically goes to the /attachments (show) page after the delete. I would like it to just refresh the page the link_to code is on.
Here is the code in the index view:
<td><%= link_to 'Delete', attachment, confirm: 'Are you sure?', method: :delete, :class => 'btn btn-mini btn-danger' %></td>
Thanks for the help!
In your attachment controller try:
def destroy
#attachment = Attachment.find(params[:id])
#attachment.destroy
respond_to do |format|
format.html { redirect_to current_page_path }
end
end
Substitute your current page path in where I put current_page_path...
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.
I've used he rails engine to create a demo app. Everything works fine, except the "Destroy" function deos not do anything.
There is no error message.
Can anyone guess as to why nothing would happen, even though there is no error message?
The function (auto-created) is:
def destroy
#term = Term.find(params[:id])
#term.destroy
respond_to do |format|
format.html { redirect_to terms_url }
format.json { head :ok }
end
end
and the HTMl part is:
<td><%= link_to 'Destroy', term, confirm: 'Are you sure?', method: :delete %></td>
I dont even get the confirmation question.
Checked, and JavaScript is enabled.
It's a pretty common question, sounds like you're not including the javascript that shows the confirmation and implements the delete method across all browsers that don't support it - did you include the rails javascript libraries?
Rails 3.1
<%= javascript_include_tag "application" %>
Rails 3
<%= javascript_include_tag :defaults %>
see link_to delete url is not working
I think your link_to signature is wrong. Try:
<%= link_to 'Destroy', term, :confirm => 'Are you sure?', :method => :delete %>
So let's say I have Posts and Comments and the url for show is /posts/1/comments/1. I want to create a link to delete that comment in the comments controller destroy method. How do I do that?
<%= link_to 'Destroy', post_comment_path(#post, comment),
data: {:confirm => 'Are you sure?'}, :method => :delete %>
in comments controller:
def destroy
#post = Post.find(params[:post_id])
#comment = Comment.find(params[:id])
#comment.destroy
respond_to do |format|
format.html { redirect_to post_comments_path(#post) }
format.xml { head :ok }
end
end
Since some time ago, the confirm option has to be included in a data hash, otherwise it will be silently ignored:
<%= link_to 'Destroy', post_comment_path(#post, comment),
data: { confirm: 'Are you sure?' }, method: :delete %>
Sometimes when you have <span>, <i> or nested elements inside of a <a> tag this way link_to use is difficult. You can inseted use raw HTML which is easy to handle, like so:
<a class="btn btn-sm" href="/blogs/<%=#blog.id%>" data-method="delete">
<i class="pg-trash"></i><span class="bold">Delete</span>
</a>
Given you have #post variable with your post object
Before Rails 7
<%= link_to 'Delete comment', post_comment_path(#post, comment),
method: :delete, data: {confirm: 'Are you sure?'} %>
Rails 7 (with Turbo, out of the box)
<%= link_to 'Delete comment', post_comment_path(#post, comment),
data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
In rails 7.0.4.2
You can use destroy method this way 'turbo gem'
<%= link_to 'Delete', user, data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
Turbo gives you the speed of a single-page web application without having to write any JavaScript. Turbo accelerates links and form submissions without requiring you to change your server-side generated HTML. It lets you carve up a page into independent frames, which can be lazy-loaded and operate as independent components. And finally, helps you make partial page updates using just HTML and a set of CRUD-like container tags. These three techniques reduce the amount of custom JavaScript that many web applications need to write by an order of magnitude. You can install 'turbo' in terminal by:
gem install turbo-rails -v 1.0.1
Or in Gemfile and bundled it.
gem 'turbo-rails', '~> 1.0', '>= 1.0.1'
Then
bundle install