# why partial doesn't work ?

**URL:** <https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973>\
**Category:** rubyonrails-talk\
**Created:** [August 8, 2009, 10:12pm UTC](https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973 "2009-08-08T22:12:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 8, 2009, 10:12pm UTC](https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973/1 "2009-08-08T22:12:53Z")

</div>

im getting: undefined local variable or method `post' for #\<ActionView::Base:0x2673624\> where in my code i have the mistake ? please help..

code at index.html.erb \<br/\> \<h1\>welcome to my weblog!\</h1\>\<br/\> \<%= render :partial =\> "posts/preview", :collection =\> @posts %\>\<br /\> \<%= link\_to 'New post', new\_post\_path %\>

code at \_preview.html.erb \<% div\_for post do %\> &nbsp;&nbsp;\<h2\>\<%= link\_to\_unless\_current h(post.title), post %\>\</h2\> &nbsp;&nbsp;\<%= truncate(post.body, :length =\> 300) %\> \<br /\>\<br /\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![Jaap\_Haagmans](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Jaap\_Haagmans](https://discuss.rubyonrails.org/u/Jaap_Haagmans)\
**Post date:** [August 8, 2009, 10:59pm UTC](https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973/2 "2009-08-08T22:59:00Z")

</div>

In your partial, you use the variable post. But post doesn't exist. At least not as far as I can see. You've sent along a variable @posts and you will probably have to use that in some kind of for loop.

For example: \<% for post in @posts do %\> &nbsp;&nbsp;\<% div\_for post do %\> &nbsp;&nbsp;&nbsp;&nbsp;\<h2\>\<%= link\_to\_unless\_current h(post.title), post %\>\</h2\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= truncate(post.body, :length =\> 300) %\> \<br /\>\<br /\> &nbsp;&nbsp;\<% end %\> \<% end %\>

Hope this helps!

Jaap Haagmans w. [http://www.relywebsolutions.nl](http://www.relywebsolutions.nl)

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 8, 2009, 11:08pm UTC](https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973/3 "2009-08-08T23:08:13Z")

</div>

jhaagmans wrote:

> In your partial, you use the variable post. But post doesn't exist. At least not as far as I can see. You've sent along a variable @posts and you will probably have to use that in some kind of for loop.
> 
> For example: \<% for post in @posts do %\> &nbsp;&nbsp;\<% div\_for post do %\> &nbsp;&nbsp;&nbsp;&nbsp;\<h2\>\<%= link\_to\_unless\_current h(post.title), post %\>\</h2\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= truncate(post.body, :length =\> 300) %\> \<br /\>\<br /\> &nbsp;&nbsp;\<% end %\> \<% end %\>
> 
> Hope this helps!
> 
> Jaap Haagmans w. [http://www.relywebsolutions.nl](http://www.relywebsolutions.nl)
> 
> On 9 aug, 00:12, Philip Gavrilos \<rails-mailing-l...@andreas-s.net\>

THANK YOU! THANK YOU! THANK YOU! WORKS FINE!

i did not understand my error here its because im newbie in ruby ( 😛 ) (if \_preview was \_post everything was fine.. i think that is because naming conventions right?)

---

<div class="post-metadata">

**Author:** ![Jaap\_Haagmans](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Jaap\_Haagmans](https://discuss.rubyonrails.org/u/Jaap_Haagmans)\
**Post date:** [August 8, 2009, 11:29pm UTC](https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973/4 "2009-08-08T23:29:34Z")

</div>

> i did not understand my error here its because im newbie in ruby ( 😛 ) (if \_preview was \_post everything was fine.. i think that is because naming conventions right?)

I'm not sure why that works. I would have to look into the code.

I recommend you start your reading up on Rails using the book "Agile Web Development with Rails". It's a wonderful book which can help you get started.

---

<div class="post-metadata">

**Author:** ![Eric9](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@Eric9](https://discuss.rubyonrails.org/u/Eric9)\
**Post date:** [August 9, 2009, 7:16am UTC](https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973/5 "2009-08-09T07:16:49Z")

</div>

It worked with \_post because Rails creates an object for the partial with the same name as the partial filename. Since you were using post.body, etc. in the partial, Rails picked up the post object and displayed everything normally. This broke when you named the partial 'preview' because you were still using post.body, etc. When you use \_preview as the partial's filename you also need to change the lines in the partial itself to preview.body, etc. in order for it to work.

Furthermore, you can change your instance variable to @previews and do:

render :partial =\> @previews

and Rails will look for a posts/\_preview.html.erb file, create a 'preview' variable for each element of the @previews object, which can then be used with same preview.body, etc. syntax in the \_preview partial as outlined above.

TIMTOWTDI, natch, but I think this all is pretty close to what you're asking about.

-eric

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 9, 2009, 8:39am UTC](https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973/6 "2009-08-09T08:39:34Z")

</div>

Eric wrote:

---

<div class="post-metadata">

**Author:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [August 9, 2009, 8:52am UTC](https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973/7 "2009-08-09T08:52:26Z")

</div>

> Eric wrote:

> > > > � \<% end %\>
> 
> > > i did not understand my error here its because im newbie in ruby ( 😛 )
> 
> > > (if \_preview was \_post everything was fine… i think that is because
> 
> > > naming conventions right?)
> 
> > 
> 
> > It worked with \_post because Rails creates an object for the partial
> 
> > with the same name as the partial filename. Since you were using
> 
> > post.body, etc. in the partial, Rails picked up the post object and
> 
> > displayed everything normally. This broke when you named the partial
> 
> > ‘preview’ because you were still using post.body, etc. When you use
> 
> > \_preview as the partial’s filename you also need to change the lines
> 
> > in the partial itself to preview.body, etc. in order for it to work.
> 
> > 
> 
> > Furthermore, you can change your instance variable to @previews and
> 
> > do:
> 
> > 
> 
> > render :partial =\> @previews
> 
> > 
> 
> > and Rails will look for a posts/\_preview.html.erb file, create a
> 
> > ‘preview’ variable for each element of the @previews object, which can
> 
> > then be used with same preview.body, etc. syntax in the \_preview
> 
> > partial as outlined above.
> 
> > 
> 
> > TIMTOWTDI, natch, but I think this all is pretty close to what you’re
> 
> > asking about.
> 
> > 
> 
> > -eric

> thanks Eric.
> 
> your article explain me tha way that partials work. simple and clean 🙂
> 
> but tight now i use the solution that Jaap told me (look above) and
> 
> works fine.
> 
> do you know the difference between them ? is Jaap solution os yours more “right” ?
> 
> –
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hi, another possibility would be to use a custom local variable that was introduced in Rails 2.2. For example,

render :partial =\> “posts/preview”, :collection =\> @posts, :as =\> :post

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![Jaap\_Haagmans](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Jaap\_Haagmans](https://discuss.rubyonrails.org/u/Jaap_Haagmans)\
**Post date:** [August 9, 2009, 11:04am UTC](https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973/8 "2009-08-09T11:04:49Z")

</div>

There is no right solution by default. My way (or equivalent) is useful when @posts is (or can be) in fact a collection of elements and you want to do the same thing for every element. If you know for a fact that there is only one element in the @posts object, Conrad's solution above will suffice and maybe even be neater. You should then call it @post though.

Eric's solution is actually the same as mine, but it deduces the partial name from the object and repeats the partial's contents for each element. If that works for you (which didn't show from your first post) you can also use that.

Hope this helps!

Jaap Haagmans w. [http://www.relywebsolutions.nl](http://www.relywebsolutions.nl)

---

<div class="post-metadata">

**Author:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [August 9, 2009, 11:48am UTC](https://discuss.rubyonrails.org/t/why-partial-doesnt-work/44973/9 "2009-08-09T11:48:52Z")

</div>

> There is no right solution by default. My way (or equivalent) is
> 
> useful when @posts is (or can be) in fact a collection of elements and
> 
> you want to do the same thing for every element. If you know for a
> 
> fact that there is only one element in the @posts object, Conrad’s
> 
> solution above will suffice and maybe even be neater. You should then
> 
> call it @post though.

I would recommend doing the following when working with collections

partials can be reused:

app/views/other/index.html.erb:

render :partial =\> “posts/preview”, :collection =\> @posts, :as =\> :post

app/views/posts/\_preview.html.erb:

\<% div\_for post do %\>

## \<%= link\_to\_unless\_current h(post.title), post %\>

\<%= truncate(post.body, :length =\> 300) %\>   
  
\<% end %\>

Now, you can reuse the partial within other views keeping your

code base DRY. The for is OK when you’re not repeating the

same fragment elsewhere. Usually, you’ll catch this during a

refactoring exercise. Lastly, Ruby for loops are implemented

as follows within an ERB template:

\<% for post in @posts %\>

…

\<% end %\>

-Conrad
