I have a Rails 3 app that is trying to perform some controller action with a button and :remote => true. At the moment, I don’t want to even worry about the success of the action, I just want to have it happen on its own, and have some onclick script that hides the element immediately.
I can’t seem to get them both to work at the same time. Here’s the code that I’m using, which does the action just fine:
<%= button_to "Ignore", url_for(:controller => "requests", :action => "ignore", :id => request.id), :remote=>true %>
Now, when I try to add the onclick, that part gets executed, but suddenly the request is treated as HTML, so I get sent to a new page instead of treating it as “remote”:
<%= button_to "Ignore", url_for(:controller => "requests", :action => "ignore", :id => request.id), :remote=>true, :onclick => "ignore('request_'"i.to_s"')" %>
As far as I can tell, the code for the button is created just fine, and the onclick works correctly, but adding that is making the request get messed up. Can anyone please help me out? Thanks a lot
Your onclick attribute has an error.
However, you should consider doing this unobtrusively.
then in the application.js
Update
The problem is that the button_to helper is building a form when you supply a url_for. To the action that you are wanting to prevent is the onsubmit not the onclick.
then in the application.js
Update #2 – change because of where the data-ignore-id attr is.
Change event to.