<% @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.
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, eg 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.