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 %>
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 %>
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?)
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.
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.
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.