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 %> -- Posted via http://www.ruby-forum.com/.
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 %> > -- > Posted viahttp://www.ruby-forum.com/.
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>THANK YOU! THANK YOU! THANK YOU! WORKS FINE! i did not understand my error here its because im newbie in ruby ( :P ) (if _preview was _post everything was fine.. i think that is because naming conventions right?) -- Posted via http://www.ruby-forum.com/.
> i did not understand my error here its because im newbie in ruby ( :P ) > (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.
jhaagmans wrote:>> i did not understand my error here its because im newbie in ruby ( :P ) >> (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.ORDERED ;) thanks for the tip. im also reading RAILS FOR DUMMIES (im stil in Dummies level :P ) -- Posted via http://www.ruby-forum.com/.
On Aug 8, 4:08 pm, Philip Gavrilos <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > THANK YOU! THANK YOU! THANK YOU! WORKS FINE! > > i did not understand my error here its because im newbie in ruby ( :P ) > (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
Eric wrote:> On Aug 8, 4:08�pm, Philip Gavrilos <rails-mailing-l...@andreas-s.net> > wrote: >> > � <% end %> >> >> i did not understand my error here its because im newbie in ruby ( :P ) >> (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. > > -ericthanks 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/.
On Sun, Aug 9, 2009 at 1:39 AM, Philip Gavrilos < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Eric wrote: > > On Aug 8, 4:08�pm, Philip Gavrilos <rails-mailing-l...@andreas-s.net> > > wrote: > >> > � <% end %> > >> > >> i did not understand my error here its because im newbie in ruby ( :P ) > >> (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/. >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> > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On 9 aug, 10:39, Philip Gavrilos <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Eric wrote: > > On Aug 8, 4:08 pm, Philip Gavrilos <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > >> > <% end %> > > >> i did not understand my error here its because im newbie in ruby ( :P ) > >> (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 viahttp://www.ruby-forum.com/.
On Sun, Aug 9, 2009 at 4:04 AM, jhaagmans <jaap.haagmans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 %> <h2><%= link_to_unless_current h(post.title), post %></h2> <%= truncate(post.body, :length => 300) %> <br /><br /> <% 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> > 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 > > On 9 aug, 10:39, Philip Gavrilos <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > Eric wrote: > > > On Aug 8, 4:08 pm, Philip Gavrilos <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > > wrote: > > >> > <% end %> > > > > >> i did not understand my error here its because im newbie in ruby ( :P > ) > > >> (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 viahttp://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---