Can't pass multiple locals when rendering JS

748 views Asked by At

I'm trying to render a javascript file (through a remote AJAX call) using the following code:

respond_to do |format|
                format.js { render "like", :locals => {:media_id => media_id, :like_type => like_type}}
end

The file is named "like.js.erb" and I know it will work because when I put just a standard javascript alert in the file, it works perfectly. The file (like.js.erb) looks like this:

<% if like_type == "l" %>
    alert("liking as <%= like_type %> for media_id <%= media_id %>");
    $('like_<%= media_id %>').update("liked");
<% elsif like_type == "u" %>
    alert("unliking as <%= like_type %> for media_id <%= media_id %>");
    $('like_<%= media_id %>').update("unliked");
<% end %>

When the file contains the code above, the POST action completes properly but nothing is returned at all. It seems that it doesn't like the multiple locals being passed.

Any ideas? Thanks!

1

There are 1 answers

3
carpamon On

If you use the :locals option then you need to provide the :partial key as a parameter.

respond_to do |format|
                format.js { render :partial => "like", :locals => {:media_id => media_id, :like_type => like_type}}
end

or:

respond_to do |format|
                format.js { render "like", :media_id => media_id, :like_type => like_type}
end