Drew Rush
Drew Rush

Reputation: 720

Passing Variable to Partial - Page Displays symbol instead of string

Here is the code in my view to call the partial:

<%= render :partial => "/divbox", :locals => { :smush => "Science" } %>

and now here is what's in _divbox.html.erb:

<div>
<h1> <%= :smush %> </h1>
</div>

I expect HTML output of:

<div>
<h1> Science </h1>
</div>

But instead I get:

<div>
<h1> smush </h1>
</div>

Thanks in advance for your time.

Upvotes: 0

Views: 839

Answers (1)

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

Change this:

<h1> <%= :class %> </h1>

To this:

<h1> <%= class %> </h1>

Note the removal of the colon. The local variables you pass into your partial are variables in the partial... not symbols.

Also.. don't use "class" at all. It's a ruby reserved word and even if it does work it's confusing. Do it like this:

<%= render :partial => "/divbox", :locals => { :class_name => "Science" } %>


<div>
<h1> <%= class_name %> </h1>
</div>

Or if you really want just "class" use "klass" which is a common substitue...

Upvotes: 3

Related Questions