Running RJS rails template from jquery get request
I am using Rails and jquery with RJS templates to make various AJAX requests.
For most of my Ajax stuff, I attach a submit handler to a form in my application.js application like this:
$('#tagging_flickr_photos').submitWithAjax();
$('#tag_submit').click(function() {
$('#flickr-photos-status').show();
});
This calls a form action that does some processing, and then is routed to the RJS template like this:
$("#flickr-photos-status").hide();
$("#flickr-photos").fadeIn();
$("#flickr-photos").html("<%= escape_javascript(render(:partial => 'flickr_photos_for_tagging_content')) %>");
It works.
Now I am trying to do the same, but only based on choosing a different value in the dropdown and not submitting the form. Here's my javascript for attaching a handler to the dropdown:
$('#film_film_name_id').change(function() {
$.get('/admin_film/make_tags?film_name_id=' + $("#film_film_name_id").val() + '&film_speed_id=' + $("#film_film_speed_id").val());
});
My controller method does some processing, then forwards it to the RJS template (make_tags.js.erb):
$("#film_tags").val(<%=@tags%>)
However, the template doesn't work. I can see the entries in my logs that it calls my method and creates the template, but no matter what I put in the template, nothing happens. I put a Javascript warning there and it doesn't fire.
I'm guessing the problem is with my Javascript handler attachment, but I can't figure out what I'm missing.
Thanks in advance for your help.
a source to share
I think you should tell jQuery that you expect JavaScript and that it should be executed. Try the following and see if it works:
$('#film_film_name_id').change(function() {
var url = '/admin_film/make_tags?film_name_id=' + $("#film_film_name_id").val() + '&film_speed_id=' + $("#film_film_speed_id").val()
$.ajax({
type: 'GET',
dataType: 'script',
url: url
});
});
Please check the jQuery docs on jQuery.ajax () if it doesn't work as I haven't tested the code.
a source to share
HI,
I am using an observer that calls directly through the prototype library (not jquery)
<td><div id="outconditionnals_container">
<% if !@milestone.howto.conditionnals.nil? then %>
<%= collection_select(:OUTconditionnal, :id, @milestone.howto.conditionnals, :id, :name, {:prompt => true}, {"index" => @outconditionnalID.to_s}) %>
<%= observe_field("OUTconditionnal_"+@outconditionnalID.to_s+"_id",
:url => { :action => :AJAX_selection_change },
:with => "'id='+value+'&dir=out&type=conditionnal&milestoneID="+@milestone.id.to_s+"'",
:on => "changed")%>
<% end %>
</div>
</td>
Then my rjs are rendered from my activity to summarize some other form elements.
Hope it helps.
a source to share
Alternatively, you can also use $. get you used.
Here's your hairstyle example:
$('#film_film_name_id').change(function() {
$.get(
'/admin_film/make_tags', // or something like '<%= make_tags_admin_film_path %>'
{
film_name_id: $("#film_film_name_id").val(),
film_speed_id: $("#film_film_speed_id").val()
},
null, // you may define an additional callback here, if that makes sense
"script"
);
});
a source to share