why partial doesn't work ?

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 %>
  <h2><%= link_to_unless_current h(post.title), post %></h2>
  <%= truncate(post.body, :length => 300) %> <br /><br />
<% end %>

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 %>
  <% div_for post do %>
    <h2><%= link_to_unless_current h(post.title), post %></h2>
    <%= truncate(post.body, :length => 300) %> <br /><br />
  <% end %>
<% end %>

Hope this helps!

Jaap Haagmans
w. http://www.relywebsolutions.nl

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 %>
  <% div_for post do %>
    <h2><%= link_to_unless_current h(post.title), post %></h2>
    <%= truncate(post.body, :length => 300) %> <br /><br />
  <% end %>
<% end %>

Hope this helps!

Jaap Haagmans
w. 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 ( :stuck_out_tongue: )
(if _preview was _post everything was fine.. i think that is because
naming conventions right?)

i did not understand my error here its because im newbie in ruby ( :stuck_out_tongue: )
(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.

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

Eric wrote:

Eric wrote:

� <% end %>

i did not understand my error here its because im newbie in ruby ( :stuck_out_tongue: )

(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 :slight_smile:

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/.

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

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

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