jason-fr04DiSC2AbAS2HK5pSfKwC/G2K4zDHf@public.gmane.org
2008-Nov-08 21:40 UTC
Conditinal layout based on controller action.
I have a very basic controller with all the restful actions. What im trying to do i my views is show a "back to links" link in every view except the index view. Im trying to keep it DRY and use some conditional statment in the application.html.erb. I cant seem to get it to work. what i tried was using the link_to_unless helper, but i cant get it to work. <%= link_to_unless({:controller => "links", :action => "index"}, "Back to links", {:action => "index" }) %> while this is rendering the Back to links text, its not creating a link and rendering it in the links controller with any action any ideas on how i can accomplish this? am i using this wrong? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Jason, jason-fr04DiSC2AbAS2HK5pSfKwC/G2K4zDHf@public.gmane.org wrote:> What im trying to do i my views is show a "back to links" > link in every view except the index view. > > Im trying to keep it DRY and use some conditional statment in the > application.html.erb.I think the helper you''re looking for is link_to_unless_current, but I see no reason your link_to_unless won''t work too. It''s just a matter of accessing the controller and / or method names responsible for calling the render. Those are given by controller.controller_name and controller.action_name and are available to you in any view. The reason you''re getting just the text rendered is that that is the spec''d behavior when the condition is false (which yours is). I believe (i.e., untested code follows) that your link_to_unless would work with... <%= link_to_unless(controller.controller_name == "links" && controller.action_name == "index", "Back to links", {:action => "index" }) %> OTOH, link_to_unless_current would just be... <%= link_to_unless_current("Back to links", {:action => "index"}) %> HTH, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---