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...
Related
I have a database with people and nicknames, nicknames are nested resources to people.
All works fine except one thing:
I am displaying a list of nicknames on the "show" page for people - next to them, I have a little link that says "delete". However I cannot delete them - I get an error as follows:
Couldn't find Person with 'id'=44
def load_person
#person = Person.find(params[:person_id])
end
The load_person method is defined in the nicknames_controller as
before_action :load_person
def load_person
#person = Person.find(params[:person_id])
end
Here's the destroy method from nicknames_controller.rb:
def destroy
#nickname.destroy
respond_to do |format|
format.html { redirect_to #person, notice: "Nickname was successfully destroyed." }
format.json { head :no_content }
end
end
Here's the view snippet of views/persons/show.html.erb:
<% #person.nicknames.each do |aka| %>
<li>
<%= link_to nickname.display_name, edit_person_nickname_path(#person, aka) %>
<%= link_to '', person_nickname_path(aka), method: :delete, data: { confirm: 'Are you sure?' }, class: "far fa-trash-alt text-error", title: "Remove" %>
</li>
<% end %>
So the route to "delete" is something like
http://127.0.0.1:3000/persons/44/nicknames/15
WHen I display the nickname (show method) and delete from there (same route as above), everything works fine.
What am I overlooking?
ok - so as simple as it was stupid. the delete link had the wrong path - it should be
<%= link_to '', person_nickname_path(#person, aka) .... %>
but I had
..person_nickname_path(aka) ...
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 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?' %>
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.