Reputation: 2460
I have a button_to helper method in a view table that I cannot get to work the way I need it. I am using it to delete a record in a different model than is building the table and I do not have the :id for but I do have other parameters that can find the proper record. Based on other questions here I think the following sytax should be the correct;
<%= button_to 'Remove',mailing_list_edit_path(:arg1 => "value1", :arg2 => "value2"),:method => :delete,:confirm => "are you sure?" %>
but I get this error when I click the button;
Routing Error
No route matches [DELETE] "/assets"
Try running rake routes for more information on available routes.
Here is the entry in my routes.rb
resources :mailing_list_edits, only: [:create, :destroy]
And the action in my controller
def destroy
MailingListEdit.where(:att1 => params[:arg1], :att2 => params[:arg2]).delete_all
respond_to do |format|
format.html { redirect_to controller1_index_path(session[:remember_token]) }
end
end
What am I doing wrong?
Upvotes: 1
Views: 402
Reputation: 2460
I found a workaround, in case it will help someone else, here it is.
The path helper would not work without an :id, so I included a dummy :id and now I am able to pass the two attributes I needed to find and destroy. So my button_to now looks like this;
<%= button_to 'Remove',mailing_list_edit_path(:id => "foobar", :arg1 => "value1", :arg2 => "value2"),:method => :delete,:confirm => "are you sure?" %>
Kind of a hack, but it works!
Upvotes: 1
Reputation: 2489
I think you don't give the object to destroy to you link. Indeed the destroy method built by the ressources is a member route: it needs the object to destroy.
For exemple:
<%= button_to 'Remove',mailing_list_edit_path(@object_to_destroy, :arg1 => "value1", :arg2 => "value2"),:method => :delete,:confirm => "are you sure?" %>
Upvotes: 3