Tony Mobily
2006-Jun-07 17:08 UTC
[Rails] Links on the left hand side - are components necessary?
Hello, Maybe this is super-trivial... I need to have a list of links on the left hand side of my application, for *every* single page. So, I''ve created a layout called "main" (main.rhtml) under view/layouts, and included >layout "main"< in pretty all of my controllers. main.rhtml has, amongst all the other HTML stuff: <%= render_component :controller=> ''dli/extras'', :action=> ''show_lookup_links''%> This is the component to display the links: class Dli::ExtrasController < ApplicationController uses_component_template_root Link=Struct.new(:name,:controller,:action) def show_lookup_links @links=[] @links << Link.new("Channels", "/lookup/channel", "index") @links << Link.new("Password durations", "/lookup/password_duration", "index") render :layout=>false end end Now: the list of links will need to change according to the user''s permission. So, there is quite a lot of logic involved here. What are my options, if I don''t want to use components? The only solution I can think of is to add a method to the general application controller (application.rb). However, how do call such an action from main.rhtml? It looks like I need to be able to specify controller _and_ action, which brings me back to components... Can somebody please enlighten me? I feel that I am heading the wrong way, and... Thanks, Merc. -- Posted via http://www.ruby-forum.com/.
Stephen Bartholomew
2006-Jun-07 17:49 UTC
[Rails] Re: Links on the left hand side - are components necessary?
Components are going out of favour anyway so you''re best off with partials: <%= render :partial => ''foo/bar'' %> then in app/views/foo/_bar.rhtml you include your code. You can add the logic into a before_filter method that creates an instance variable that the partial can access. You could also create a helper method in app/helpers/application_helper.rb: def my_helper(options = {}) # make code end Then you can just call it in your templates: <%= my_helper :option1 => ''foo'', :option2 => ''bar'' %> The first method is probably best though. If you''re generating a list of links, i would create a before_filter in application_controller that loads the links into a collection then pass this to the partial: - application_controller.rb before_filter :load_menu def load_menu # load links into @links end - main.rhtml <ul> <%= render :partial => ''link'', :collection => @links %> </ul> - _link.rhtml <li><%= link %></li> Check out http://api.rubyonrails.org/classes/ActionController/Base.html#M000206 for more information about partials. Hope that helps, Steve Tony Mobily wrote:> Hello, > > Maybe this is super-trivial... > I need to have a list of links on the left hand side of my application, > for *every* single page. > > So, I''ve created a layout called "main" (main.rhtml) under > view/layouts, and included >layout "main"< in pretty all of my > controllers. > > main.rhtml has, amongst all the other HTML stuff: > > <%= render_component :controller=> ''dli/extras'', :action=> > ''show_lookup_links''%> > > This is the component to display the links: > > class Dli::ExtrasController < ApplicationController > uses_component_template_root > Link=Struct.new(:name,:controller,:action) > > def show_lookup_links > @links=[] > @links << Link.new("Channels", "/lookup/channel", "index") > @links << Link.new("Password durations", > "/lookup/password_duration", "index") > render :layout=>false > end > > end > > Now: the list of links will need to change according to the user''s > permission. So, there is quite a lot of logic involved here. > > What are my options, if I don''t want to use components? > > The only solution I can think of is to add a method to the general > application controller (application.rb). However, how do call such an > action from main.rhtml? It looks like I need to be able to specify > controller _and_ action, which brings me back to components... > > Can somebody please enlighten me? I feel that I am heading the wrong > way, and... > > Thanks, > > Merc.-- Posted via http://www.ruby-forum.com/.
James Ludlow
2006-Jun-07 17:53 UTC
[Rails] Links on the left hand side - are components necessary?
On 6/7/06, Tony Mobily <merc@mobily.com> wrote:> def show_lookup_links > @links=[] > @links << Link.new("Channels", "/lookup/channel", "index") > @links << Link.new("Password durations", > "/lookup/password_duration", "index") > render :layout=>false > endThis looks like trouble. You''re going to be better off if you use link_to() and let Rails build the paths that it needs. When I see "/lookup/channel" hardcoded it sets off warning bells.> Can somebody please enlighten me? I feel that I am heading the wrong > way, and...One thing that you could do to get away from using a component would be to put the common pieces of your nav as partials in /app/view/shared. Your layout(s) could then use this shared nav RHTML code as needed. -- James
Brian Hogan
2006-Jun-07 17:55 UTC
[Rails] Re: Links on the left hand side - are components necessary?
What''s wrong with just putting <%=link_to_unless_current "Home", :controller=>"main", :action=>"index" %> in your main.rhtml file If they''re always the same across all pages, why make the extra work? If you need them to be dynamically generated, then use render :partial as stated in an earlier post. On 6/7/06, Stephen Bartholomew <steve@curve21.com> wrote:> Components are going out of favour anyway so you''re best off with > partials: > > <%= render :partial => ''foo/bar'' %> > > then in app/views/foo/_bar.rhtml you include your code. You can add the > logic into a before_filter method that creates an instance variable that > the partial can access. > > You could also create a helper method in > app/helpers/application_helper.rb: > > def my_helper(options = {}) > # make code > end > > Then you can just call it in your templates: > <%= my_helper :option1 => ''foo'', :option2 => ''bar'' %> > > The first method is probably best though. If you''re generating a list > of links, i would create a before_filter in application_controller that > loads the links into a collection then pass this to the partial: > > - application_controller.rb > before_filter :load_menu > > def load_menu > # load links into @links > end > > - main.rhtml > <ul> > <%= render :partial => ''link'', :collection => @links %> > </ul> > > - _link.rhtml > <li><%= link %></li> > > Check out > http://api.rubyonrails.org/classes/ActionController/Base.html#M000206 > for more information about partials. > > Hope that helps, > > Steve > > Tony Mobily wrote: > > Hello, > > > > Maybe this is super-trivial... > > I need to have a list of links on the left hand side of my application, > > for *every* single page. > > > > So, I''ve created a layout called "main" (main.rhtml) under > > view/layouts, and included >layout "main"< in pretty all of my > > controllers. > > > > main.rhtml has, amongst all the other HTML stuff: > > > > <%= render_component :controller=> ''dli/extras'', :action=> > > ''show_lookup_links''%> > > > > This is the component to display the links: > > > > class Dli::ExtrasController < ApplicationController > > uses_component_template_root > > Link=Struct.new(:name,:controller,:action) > > > > def show_lookup_links > > @links=[] > > @links << Link.new("Channels", "/lookup/channel", "index") > > @links << Link.new("Password durations", > > "/lookup/password_duration", "index") > > render :layout=>false > > end > > > > end > > > > Now: the list of links will need to change according to the user''s > > permission. So, there is quite a lot of logic involved here. > > > > What are my options, if I don''t want to use components? > > > > The only solution I can think of is to add a method to the general > > application controller (application.rb). However, how do call such an > > action from main.rhtml? It looks like I need to be able to specify > > controller _and_ action, which brings me back to components... > > > > Can somebody please enlighten me? I feel that I am heading the wrong > > way, and... > > > > Thanks, > > > > Merc. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Kevin Olbrich
2006-Jun-07 17:58 UTC
[Rails] Re: Links on the left hand side - are components necessary?
On Wednesday, June 07, 2006, at 7:48 PM, Stephen Bartholomew wrote:>Components are going out of favour anyway so you''re best off with >partials: > ><%= render :partial => ''foo/bar'' %> > >then in app/views/foo/_bar.rhtml you include your code. You can add the >logic into a before_filter method that creates an instance variable that >the partial can access. > >You could also create a helper method in >app/helpers/application_helper.rb: > >def my_helper(options = {}) > # make code >end > >Then you can just call it in your templates: ><%= my_helper :option1 => ''foo'', :option2 => ''bar'' %> > >The first method is probably best though. If you''re generating a list >of links, i would create a before_filter in application_controller that >loads the links into a collection then pass this to the partial: > >- application_controller.rb >before_filter :load_menu > >def load_menu > # load links into @links >end > >- main.rhtml ><ul> > <%= render :partial => ''link'', :collection => @links %> ></ul> > >- _link.rhtml ><li><%= link %></li> > >Check out >http://api.rubyonrails.org/classes/ActionController/Base.html#M000206 >for more information about partials. > >Hope that helps, > >Steve > >Tony Mobily wrote: >> Hello, >> >> Maybe this is super-trivial... >> I need to have a list of links on the left hand side of my application, >> for *every* single page. >> >> So, I''ve created a layout called "main" (main.rhtml) under >> view/layouts, and included >layout "main"< in pretty all of my >> controllers. >> >> main.rhtml has, amongst all the other HTML stuff: >> >> <%= render_component :controller=> ''dli/extras'', :action=> >> ''show_lookup_links''%> >> >> This is the component to display the links: >> >> class Dli::ExtrasController < ApplicationController >> uses_component_template_root >> Link=Struct.new(:name,:controller,:action) >> >> def show_lookup_links >> @links=[] >> @links << Link.new("Channels", "/lookup/channel", "index") >> @links << Link.new("Password durations", >> "/lookup/password_duration", "index") >> render :layout=>false >> end >> >> end >> >> Now: the list of links will need to change according to the user''s >> permission. So, there is quite a lot of logic involved here. >> >> What are my options, if I don''t want to use components? >> >> The only solution I can think of is to add a method to the general >> application controller (application.rb). However, how do call such an >> action from main.rhtml? It looks like I need to be able to specify >> controller _and_ action, which brings me back to components... >> >> Can somebody please enlighten me? I feel that I am heading the wrong >> way, and... >> >> Thanks, >> >> Merc. > > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsThis is basically how I have done it. A partial to display the links. In my case, I am using the user_engine/login_engine combo, so all I have to do is run a query against the permissions table to get a list of actions that are allowed. It''s not perfect as it is because there are several actions that you really wouldn''t want users to call directly (i.e., an ''edit'' action needs an id to work). Another approach would be to pre-generate a list of menu items and then use ''link_if_authorized'' instead of ''link_to''. This will make them show up only if the user has permission to access them (it''s included in the user_engine). _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
Tony Mobily
2006-Jun-08 01:34 UTC
[Rails] Re: Re: Links on the left hand side - are components necessa
Hello, Brian Hogan wrote:> What''s wrong with just putting > > <%=link_to_unless_current "Home", :controller=>"main", :action=>"index" > %> > > in your main.rhtml file > > If they''re always the same across all pages, why make the extra work?>From my post:"Now: the list of links will need to change according to the user''s permission. So, there is quite a lot of logic involved here."> If you need them to be dynamically generated, then use render :partial > as stated in an earlier post.Yep - that''s exactly what I''m doing. Merc. -- Posted via http://www.ruby-forum.com/.
Tony Mobily
2006-Jun-08 04:33 UTC
[Rails] Re: Links on the left hand side - are components necessary?
Hi,>> def show_lookup_links >> @links=[] >> @links << Link.new("Channels", "/lookup/channel", "index") >> @links << Link.new("Password durations", >> "/lookup/password_duration", "index") >> render :layout=>false >> end > > This looks like trouble. You''re going to be better off if you use > link_to() and let Rails build the paths that it needs. When I see > "/lookup/channel" hardcoded it sets off warning bells.Oh no, it''s not. /lookup/channel is _actually_ a contoller''s name which lives in a separate directory (I put a whole bunched of controllers under "lookup/..." and called them Lookup::ChannelController, for example). Merc. -- Posted via http://www.ruby-forum.com/.