Rails newbie: why is my partial template not rendering?

D

dan.caugherty

Hi there,

I wanted to be a good Rails programmer, and use a shared partial
template for
common view logic (in this case, displaying information about users).

The view code that uses the render() call looks (something) like this:

<p>And now, the hat sizes of our users... </p>
<% render:)partial => "shared/user", :locals => { :user => @user })
-%>

...and my app/views/shared/user.rthml file looks like:

<p><%=h user.name -%></p><p><%=h user.hat_size -%></p>

Now, the files are named and located where they should be as far as I
can tell,
but I never see my users' names or hat sizes rendered. Viewing the HTML
source
of the resulting page shows.. nothing at all. Nothing is rendered,
maybe not even
whitespace!

Any ideas as to what's going on? What am I missing? I'm stumped.

Thx in advance,
-- Dan C.
 
L

Logan Capaldo

Hi there,

I wanted to be a good Rails programmer, and use a shared partial
template for
common view logic (in this case, displaying information about users).

The view code that uses the render() call looks (something) like
this:

<p>And now, the hat sizes of our users... </p>
<% render:)partial => "shared/user", :locals => { :user => @user })
-%>

..and my app/views/shared/user.rthml file looks like:

<p><%=h user.name -%></p><p><%=h user.hat_size -%></p>

Now, the files are named and located where they should be as far as I
can tell,
No they aren't ;)

it has to be app/views/shared/_user.rhtml to be a partial.
 
M

Max Muermann

The view code that uses the render() call looks (something) like this:
<p>And now, the hat sizes of our users... </p>
<% render:)partial => "shared/user", :locals => { :user => @user })
-%>

Ah, I've done this many many times... You're missing a "="

<% render ...

should be
<%= render ...

And the partial file name needs to start with an underscore.

Cheers,
Max
 
M

Matt Todd

Both Logan's and Max's answers are right on the money. That should set
you up just fine.

I'd definitely join this mailing list as it will be much more focused
on Rails. (And, besides, who likes hearing messages about posting
questions on the other list anyhow? Hehehe.)

By the by, did you know you don't have to put the ()s in the message passing?

<%= render:)partial => "shared/user", :locals => { :user => @user }) -%>

could be

<%= render :partial => "shared/user", :locals => { :user => @user } -%>

which is a bit cleaner in my opinion. (That whole signal verses noise thing.)

Cheers,

M.T.

P.S. -- Come to think of it, I'm not sure if you have to specify
"shared/_user" or just "shared/user" as your partial template. Hmm.
 
D

dan.caugherty

Thank you everyone for showing me the error of my ways
(the missing =, and alerting me to the presence of the Rails group).

And yes, this topic does need to be in a FAQ somewhere. Argh!

Cheers,
-- Dan C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top