Take the 2-minute tour ×

I'm trying to pass in the variable "post" to a partial. The partial is being used on both my show#view & I'm also rendering a collection using it. Here's what it looks like (notice the "@"):

<%= render 'my_partial/my_view', post: @post %>
##Collection ## (I'm not using the "@" symbol)
<%= render 'my_partial/my_view', post: post %>


<% if @post.something? %>
   ## do this
<% else %>
   ## do that
<% end %>

And then I get the beautiful NoMethodError undefined methodsomething?' for nil:NilClass` page when using it in my collection. I know why I'm getting it, I'm just wondering what's the DRY way(s) of getting this to work? Should I just create another partial?

Thank you

Gave my solution below.. Though, it's probably not the best way...

share|improve this question
Your partial must reference post, which is the "local" you're defining, not @post, which is an instance variable and undefined in the case of a collection. –  meagar Sep 23 '13 at 15:48
Hey @meagar I just read some of you're comments & I totally get it now but I just can't seem to figure out why I'm getting this error. I went ahead & accepted the answer you said was correect but could you shed some light as to y I might be getting this error still? Thanks –  goo Sep 23 '13 at 16:03
@meagar nvm, I think I know what it is.. I think it's because I'm using a permalink as the id.. Thanks again –  goo Sep 23 '13 at 16:26

2 Answers 2

up vote 1 down vote accepted

Why are you referencing @post in your partial? You should use post instead, that is the entire point of what you are doing (passing variables to a partial as local variables).

share|improve this answer
What? Why does it matter? –  Logan Serman Sep 22 '13 at 16:47
What do you think the post: @post and post: post are doing? They are telling your partial what the post variable refers to. So why are you referencing @post in your partial instead of post, when @post is not defined in the collection context? The whole point of what you are doing is to define a local variable (post) so that in both the singular & collection cases the partial can render given the appropriate variable. –  Logan Serman Sep 22 '13 at 22:05
I understand exactly what you are doing. You just don't understand what I'm telling you. I have made it as clear as I possibly could. Good luck. –  Logan Serman Sep 23 '13 at 15:32
@Jon This answer is correct; use post in your partial, not @post. This is how locals work, when passed to a partial, you're telling it what variables to define, and what their value is. The @post is the value, the post is the name of the variable. Your partial needs to use post. –  meagar Sep 23 '13 at 15:43
I know you're right. This is what I was initially doing & after rereading the rails docs to confirm it, I think the problem is since I'm using a permalink as my id, it's passing my permalink instead of my actual id. Maybe this will help someone else with this problem, but thank you :) (I deleted my other replies so this reply will be more noticeable) –  goo Sep 23 '13 at 16:29

You must change the rendering to:

<%= render 'my_partial/my_view', locals: { post: @post } %>

Take a look in the rails guide: http://guides.rubyonrails.org/layouts_and_rendering.html

Search for "3.4.4 Passing Local Variables" and you will find more information about this whole stuff.

And for fixing your collection problem also check out the rails guide and search for: "3.4.5 Rendering Collections".

share|improve this answer
This hasn't been true for quite a while. You don't need to explicitly use locals: { ... } anymore, Rails will handle render "partial", variable: @variable just fine, treating variable: @variable as the locals option. –  meagar Sep 23 '13 at 15:42
@meagar I just copy that from the official rails guide :). You can take a look at the guide. So it is not false, but maybe your style is newer. –  Mattherick Sep 23 '13 at 16:14

Your Answer


