2

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?

2
  • It's ERB. Didn't you miss something? It should be <%= @submission.title %>. Commented Sep 6, 2013 at 5:38
  • @Casper - Yup, I feel like an idiot for forgetting. Thanks! Commented Sep 6, 2013 at 5:44

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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>

3 Comments

Please ear reputation points so by answering in proper places.
Carnway, I don't believe the last part is necessarily correct; if he is not using the :locals hash, then he would still need the @. Unless I am missing something? If he did use :local, then yes he could access it as you've described.
The last part is assuming he is going to pass the variable via the locals hash which means inside the partial the submission variable refers to @submission defined in the controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.