How can I guess the view''s name in my layout method? It would be great to know because of highlight the current menu item the user has selected. Of course I can store it in a variable in my controller just before I call render :view, but I hope there''s a much more sexy way to do this. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/camping-list/attachments/20111013/6d1681f2/attachment.html>
On Thu, Oct 13, 2011 at 21:33, Nokan Emiro <uzleepito at gmail.com> wrote:> How can I guess the view''s name in my layout method? > > It would be great to know because of highlight the current > menu item the user has selected.? Of course I can store it in > a variable in my controller just before I call render :view, but I > hope there''s a much more sexy way to do this.If you''re using Markaby (that is, templates written in Ruby under MyApp::Views) and Ruby 1.9 you can use __method__: module App::Views def foo p "I''m inside: #{__method__}" end end But you can''t use that in a layout or partial though (it would just return "layout" or the name of the partial)?
> > > > How can I guess the view''s name in my layout method? > > If you''re using Markaby (that is, templates written in Ruby under > MyApp::Views) and Ruby 1.9 you can use __method__: > > module App::Views > def foo > p "I''m inside: #{__method__}" > end > end > > But you can''t use that in a layout or partial though (it would just > return "layout" or the name of the partial)? >My question was about the layout. Suppose that Alfa and Beta are menu items in the layout, and I want to mark the current menu item with different appearance: module App::Views def layout case ..... #<--- Which view am I currently rendering? :alfa or :beta? when :alfa # generate an alfa-selected menu when :beta # generate a beta-item-selected menu end div(:class => :content) { self << yield } end def alfa "Alfa" end def beta "Beta" end end -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/camping-list/attachments/20111013/d3c6d24d/attachment.html>
On Thu, Oct 13, 2011 at 23:36, Nokan Emiro <uzleepito at gmail.com> wrote:> My question was about the layout.? Suppose that Alfa and Beta are > menu items in the layout, and I want to mark the current menu item > with different appearance: > > > module App::Views > ? def layout > ??? case .....? #<--- Which view am I currently rendering? ? :alfa or :beta? > ????? when :alfa > ??????? # generate an alfa-selected menu > ????? when :beta > ??????? # generate a beta-item-selected menu > ??? end > ??? div(:class => :content) { > ?? ?? self << yield > ??? } > ? end > > ? def alfa > ??? "Alfa" > ? end > > ? def beta > ??? "Beta" > ? end > endYou could always override the render-method yourself: module App::Helpers def render(name, *rest) @template_name = name.to_s unless name == :layout super end end module App::Views def layout h1 "I''m in #{@template_name}" self << yield end end