Tom Maxwell
Tom Maxwell

Reputation: 9573

Pass Rails variable through AJAX to partial?

In my Rails app I'm passing a variable to this call: $.get("/parent_submissions/", {submission: d.id}, null, "script");, and I'd like the submission variable to pass through to the partial that is loaded through the controller. The controller action that is routed through /parent_submissions/ is:

def parent
    @submission = Submission.find(params[:submission])
end

That action loads parent.js.erb, which looks like this:

$("body").append( "<%=j render :partial => 'parent', :locals => {:submission => @submission } %>" );

And _parent.html.erb simply looks like this:

<h1>@submission.title</h1>

I checked in my console to see what happens when the get request is called, and it looks like this:

Processing by SubmissionsController#parent as JS
Parameters: {"submission"=>"190", "_"=>"1378443591336"}

This means that the submission value is not null, and is passing the correct submission id through the request correctly. But when the partial is rendered, it just looks like @submission.title.

I assume something is wrong in the controller. Any ideas?

Upvotes: 2

Views: 2142

Answers (2)

Camway
Camway

Reputation: 1062

if your going to use the instance variable there is not reason for you to pass it in the locals hash.

So this line:

$("body").append( "<%=j render :partial => 'parent', :locals => {:submission => @submission } %>" );

Could just be:

$("body").append( "<%=j render :partial => 'parent' %>" );

If you want to use the locals your partial needs to be the following:

<h1><%= submission.title %></h1>

Upvotes: 1

saihgala
saihgala

Reputation: 5774

You need to change <h1>@submission.title</h1> to <h1><%= @submission.title %></h1> in your partial.

Upvotes: 3

Related Questions