2
<% @services.each do |service| %>
    <div class="widget-content nopadding">
        <ul class="activity-list">
            <li>
                <%= link_to service.company.name, "#", :id => 'specific_service' %>
            </li>
        </ul>

    </div>
<% end %>

So, I have a list of various services which all have an id. the link_to displays the service's company name which performs the 'specific_service' javascript function. But I want it to display unique data according to the service's id. How do I pass in the service.id to the link_to in order for the controller to have access to the correct service?

EDIT

$('#specific_service').click(function(){
    var id = $(this).data('id');
    var _url = '<%= dashboard_specific_service_path(:format => :js) %>?start_date=' + encodeURI($('#start_date').val()) + '&end_date=' + encodeURI($('#end_date').val()) + '&service_id=' + id;
    $.ajax({
      type: 'GET',
      url: _url,
      dataType : 'script'
    });
});

This is my javascript function which invokes a file called 'specific_service.js.erb' which would then render a partial called 'specific_service'. The thing is I don't know if I'm using the var id correctly.

2
  • I am not entirely sure what you are asking. But if you want to send data to the controller, pass it as a query parameter Commented Nov 13, 2013 at 16:39
  • so you know how you pass parameters for urls normally (i.e. some_path(@object)). I essentially want to do the same thing except I'm not going to a particular path, I'm just reloading the current page. So in pseudocode I want to do something like "#<%=service.id%>" as the path so that the controller can use the id information. Commented Nov 13, 2013 at 16:47

1 Answer 1

5

You can use data- attributes to pass arbitrary values to a HTML element:

link_to service.company.name,           # e.g. "ACME"
        '#',
        :class => 'specific_service', 
        :data => {:id => service.id}    # e.g. 12345

which will result in something like this:

<a href="#" class="specific_service" data-id="12345">ACME</a>

this can be retrieved inside the Javascript, e.g. with jQuery:

$('.specific_service').click(function() {
  var id = $(this).data('id');

  // now do something with the id
});

note that i used :class not :id because the element can occur multiple times.

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

9 Comments

but the javascript function must be referenced using :id => 'specific_service', otherwise it doesn't work.
Multiple occurences of the same HTML id are invalid markup. Some browsers might interpret this as you'd expect, others not. This may break your functionality, e.g. the click event might only be added to one of the links or none if you're unlucky. You will never know if your app works. What do you mean it "must" be that way?
I edited my question for more info. maybe it'll help clear things up
i think you need to use &id= not &service_id=
ok, i got it to work. but it only works for the first service on the list. would you know what's causing it not to work for other services? btw thanks for the help!
|

Your Answer

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