3

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.

2
  • Your javascript isn't in a javascript tag, and you reference _id instead of id in the first function. Commented May 16, 2014 at 13:54
  • ya its under javascript tag only ... k I am updating Commented May 16, 2014 at 13:56

2 Answers 2

10

@max-williams answer should solve your problem. But I'd also recommend that you explore the option of embedding the record ID in the <tr> tag as a data attribute instead of writing out the onclick function. Then in your JS, you can dynamically select the ID in your event listeners.

Consider:

<tr data-id="<%= vendor.id %>">
  <td><%= link_to vendor.id, '#', :class=>"vendor-link" %></td>
</tr>

Then just listen for clicks:

$( ".vendor-link" ).click(function() {
  id = $(this).closest("tr").data("id");
  alert(id);
});

This method will separate out your JS from your HTML template and make it a little more manageable. Plus, you can also attach multiple click functions, giving you a little more flexibility.

Sign up to request clarification or add additional context in comments.

1 Comment

This is true, it's nicer to not have onclick attributes in your template code, and instead collect all of your JS together.
3

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.

12 Comments

And what happens? Any javascript errors? Anything at all?
in console vendor is not defined
I'd expect that since vendor is a variable in rails, not in javascript. You do understand that this html and javascript is generated on the server, but runs in the browser right?
erb tags like this <%= link_to vendor.id, '#', :onclick => "func2(#{vendor.id})" %> are evaluated (aka rendered) on your server, in rails, to make a static page of text to send back to the browser. The browser then runs this page. So vendor in the above example is a variable in rails, on the server. The generated text will be something like <a href="#" onclick="func2(123)">123</a>. vendor is not defined anywhere on the resulting page of text and so cannot possibly be referenced by any javascript.
That's referencing my answer by the way (which should work), rather than your broken question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.