Hi Rails group, I understand the basic CRUD scaffold setup. I''m trying to figure out the best way to implement the default index behavior with a model on every page as navigation. I''m having a conflict implementing both List.all and List.new on the same page. For example, in create new, a basic call to the controller would include: <%= form_for @list do |form| %> <label>name: <p><%= form.text_field :name %></p> </label> <p><%= form.submit %></p> <% end %> The controller is: def new @list = List.new end The problem I have is how to also show the List.all on the same page. The DEF index uses @lists = List.all for the function. I can''t invoke @list on the same page to both equal List.new and List.all. Is the best practice to create a different method in the controller (set it as a before_filter so I can show on all views) to show all? I''ve created the following to test it out at in the controller: before_filter :nav_show_lists def nav_show_lists respond_with(@nav_lists = List.all) end Then it is invoked in the application.html.erb view: <% @nav_lists.each do |list| %> <li><%= link_to "#{list.name}", list_url(list) %></li> <% end %> Thanks -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rex, You could set @lists and @list in the new action (they are separate vars), even though you stated you couldn''t. However, if I understand what you''re trying to achieve (assign @lists for all views) then a before filter is fine. You can place it in a specific controller or the application controller (so it''s available for all pages in your app, as it sounds like it''s used in Navigation). You should think about caching, so you''re not hitting the DB each time an action is called. There are caching schemes out there to look into. before_filter :nav_show_lists def nav_show_lists @nav_lists = List.all end Brian -- Posted via http://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
This applies to all newbies: It''s important to get your words right. If you''re not going to do this, or you can''t, then use more words rather than less and show actual code. Sentences like "a model on every page for navigation" don''t make sense. Use your own words rather than technical ones. It''s always better. Blog: http://random8.zenunit.com/ Twitter: http://twitter.com/random8r Learn: http://sensei.zenunit.com/ New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy process! Check it out now! On 04/03/2011, at 5:50 PM, Rex <rexfeng-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Rails group, > > I understand the basic CRUD scaffold setup. I''m trying to figure out > the best way to implement the default index behavior with a model on > every page as navigation. I''m having a conflict implementing both > List.all and List.new on the same page. > > For example, in create new, a basic call to the controller would > include: > <%= form_for @list do |form| %> > <label>name: > <p><%= form.text_field :name %></p> > </label> > <p><%= form.submit %></p> > <% end %> > > The controller is: > def new > @list = List.new > end > > The problem I have is how to also show the List.all on the same page. > The DEF index uses @lists = List.all for the function. I can''t invoke > @list on the same page to both equal List.new and List.all. > > Is the best practice to create a different method in the controller > (set it as a before_filter so I can show on all views) to show all? > > I''ve created the following to test it out at in the controller: > before_filter :nav_show_lists > > def nav_show_lists > respond_with(@nav_lists = List.all) > end > > Then it is invoked in the application.html.erb view: > <% @nav_lists.each do |list| %> > <li><%= link_to "#{list.name}", list_url(list) %></li> > <% end %> > > Thanks > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Brian, Instead of using the respond_with, @nav_lists = List.all works. Thanks Julian, Thanks for the tip. You are right about not mixing up technical terms. On Mar 4, 9:45 am, Brian Ledsworth <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Rex, > > You could set @lists and @list in the new action (they are separate > vars), even though you stated you couldn''t. However, if I understand > what you''re trying to achieve (assign @lists for all views) then a > before filter is fine. You can place it in a specific controller or the > application controller (so it''s available for all pages in your app, as > it sounds like it''s used in Navigation). > > You should think about caching, so you''re not hitting the DB each time > an action is called. There are caching schemes out there to look into. > > before_filter :nav_show_lists > > def nav_show_lists > @nav_lists = List.all > end > > Brian > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
On Mar 4, 2011, at 8:48 PM, Julian Leviston wrote:> This applies to all newbies: It''s important to get your words right. > If you''re not going to do this, or you can''t, then use more words > rather than less and show actual code. Sentences like "a model on > every page for navigation" don''t make sense. Use your own words > rather than technical ones. It''s always better.I agree with this in principle, but it''s important to remember that a lot of our "technical" words have real-life definitions: a model may be found in a hobby store, or on a catwalk... (I''m too sexy for your Rails, too sexy...) 8-) Walter> > Blog: http://random8.zenunit.com/ > Twitter: http://twitter.com/random8r > Learn: http://sensei.zenunit.com/ > New video up now at http://sensei.zenunit.com/ real fastcgi rails > deploy process! Check it out now! > > > On 04/03/2011, at 5:50 PM, Rex <rexfeng-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hi Rails group, >> >> I understand the basic CRUD scaffold setup. I''m trying to figure out >> the best way to implement the default index behavior with a model on >> every page as navigation. I''m having a conflict implementing both >> List.all and List.new on the same page. >> >> For example, in create new, a basic call to the controller would >> include: >> <%= form_for @list do |form| %> >> <label>name: >> <p><%= form.text_field :name %></p> >> </label> >> <p><%= form.submit %></p> >> <% end %> >> >> The controller is: >> def new >> @list = List.new >> end >> >> The problem I have is how to also show the List.all on the same page. >> The DEF index uses @lists = List.all for the function. I can''t invoke >> @list on the same page to both equal List.new and List.all. >> >> Is the best practice to create a different method in the controller >> (set it as a before_filter so I can show on all views) to show all? >> >> I''ve created the following to test it out at in the controller: >> before_filter :nav_show_lists >> >> def nav_show_lists >> respond_with(@nav_lists = List.all) >> end >> >> Then it is invoked in the application.html.erb view: >> <% @nav_lists.each do |list| %> >> <li><%= link_to "#{list.name}", list_url(list) %></li> >> <% end %> >> >> Thanks >> >> -- >> 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 >> . >> > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 > . >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.