Reputation: 3295
I've seen many guides related to this topic like this one that are all focused around rendering a partial when a user clicks a link that you direct to a specific controller method.
What if a link click isn't my triggering event? In my case, if a user selects an item from a dropdown, I want to render a partial using data from what they selected.
Here is my non-working code -
// this is the event that is fired when a user clicks on a suggestion
$('.typeahead').bind('typeahead:selected', function(event, datum, name) {
$('#my-list').html("<%= escape_javascript(render(:partial => 'some_block')) %>");
});
If I name this file "blah.js" it simply renders the Ruby code as a string, if I name it "blah.js.erb", it gives an error saying there's no render
method for the class...
Do I need to make a specific AJAX call within the event function?
Upvotes: 0
Views: 347
Reputation: 351
Yes, you need ajax call for this, 2 reasons, first you don't have any REST event associated with your render so you can't use js.erb extension and second you can't render using .js extension
So, what you need is to fetch data, to fetch data from server and display you need to use
$.get(function(){
url: ''
data: ''
success: ''
}); function of ajax.
See this documentation
https://api.jquery.com/jquery.get/
Upvotes: 1