Passing id of an object through an onclick function of jquery in Ruby On Rails
javascript, jquery, ruby, ruby-on-rails, ruby-on-rails-3
Solution
I think you just need to escape the contents of your onclick to have the actual id in it, instead of just a string saying "vendor.id" - remember it's just a string, so it needs to have the right data in it when it gets loaded in the browser.
Try
<td><%= link_to vendor.id, '#', :onclick => "func2(#{vendor.id})" %></td>
Note double quotes, to allow string interpolation.
Problem
I have one Controller `VendorController`. And view file is `index.html.erb` I want to pass the id of vendor through onclick function. Here is my code Codes under `vendors_controller.rb` ``` def index @vendor = Vendor.all end ``` Codes of `index.html.erb` ``` <script language="javascript" type="text/javascript"> function func1(id) { alert(_id); } function func2(id) { alert(id); } </script> <% @vendors.each do |vendor| %> <tr> <td><%=link_to vendor.name , '#', onclick: "func1(1)", remote: true %></td> <td><%= link_to vendor.id, '#', :onclick => 'func2(vendor.id)' %></td> </tr> <% end %> ``` For `func1()` i am getting the alert. But for `func2` I am not getting any response. Please help..thank you in advance.