:onclick doesn't work in button_to

2019-06-27 18:44发布

im trying to call a function through onclick method of a button. here is the code:

      <%= button_to "Close" , :onclick => "show_back(), return false" %>

but it doesn't work, everytime i click the button an error occours

" No action responded to 29. Actions: checkout, create, destroy, edit, find_cart, index, new, save_order, show, and update "

how ever when i impliment the same thing in a href it works

     <a onclick="show_back();return false;" href="#."> Close </a>

can anyone plz tell me what im doing wrong, thanks in advance..

3条回答
forever°为你锁心
2楼-- · 2019-06-27 19:17

you can try this

=link_to "value", "javascript:function()"

and add your class, if you use like bootstrap, you can try:

=link_to "value", "javascript:function()", class: 'btn btn-default'
查看更多
▲ chillily
3楼-- · 2019-06-27 19:23

This worked for me. the key was realizing that it has to be the html_options hash

<%= button_to "Click", designer_path(@current_user), { :onclick => "debugger", :class=>"button", :method => :put }  %>
查看更多
我只想做你的唯一
4楼-- · 2019-06-27 19:33

I was looking for something similar. My problem was that I wanted to fire an addCookie() function when a user click logged in.

Did that with:

<%= submit_tag 'Go', class: "ses_submit_btn", :onclick => "addCookie()" %>
查看更多
登录 后发表回答