Rails 视图:创建菜单

发布于 2024-11-07 10:03:12 字数 1012 浏览 0 评论 0原文

我遇到了一个奇怪的问题。我正在显示一个视图,人们可以在其中按不同范围浏览照片,并且我的视图中有以下菜单:

#photo_browser
    = link_to 'Recent', browse_photos_path(:view=>'recent'), :class => 'button'
    = link_to 'Best Photograhy', browse_photos_path(:view=>'best'), :class => 'button'
    = link_to 'Most Loved Places', browse_photos_path(:view=>'loved'), :class => 'button'
    = link_to 'Flagged', browse_photos_path(:view=>'flagged'), :class => 'button' if user_signed_in? && current_user.has_role?(:admin)

因此,当前选定的视图是 url 中的一个参数(即 photos/browse?view=recent )。 现在,我想向当前视图的任何链接添加一个“选定”类。问题是我一直想不出一个好的方法来做到这一点。我可以为每个链接做类似...

- @presenter.view == recent? recentClass = 'selected' : recentClass = nil
= link_to 'Recent', browse_photos_path(:view=>'recent'), :class => 'button ' + recentClass

...的事情,但这看起来非常冗长并且有点草率。

那么,我的问题是,有没有更好的方法来处理这种情况?

那么,处理创建这些菜单的任何代码最好都存在于视图文件中、助手中、Presenter 模型中或其他东西中吗?

感谢您的帮助!

I've run into kind of an odd issue. I'm showing a view where people can browse photos by different scopes, and I have the following menu in my view:

#photo_browser
    = link_to 'Recent', browse_photos_path(:view=>'recent'), :class => 'button'
    = link_to 'Best Photograhy', browse_photos_path(:view=>'best'), :class => 'button'
    = link_to 'Most Loved Places', browse_photos_path(:view=>'loved'), :class => 'button'
    = link_to 'Flagged', browse_photos_path(:view=>'flagged'), :class => 'button' if user_signed_in? && current_user.has_role?(:admin)

So, the currently selected view is a param in the url (ie.photos/browse?view=recent).
Now, I'd like to add a "selected" class to whatever link is the current view. The problem is I haven't been able to think of a good way to do this. I could do something like...

- @presenter.view == recent? recentClass = 'selected' : recentClass = nil
= link_to 'Recent', browse_photos_path(:view=>'recent'), :class => 'button ' + recentClass

...for each link, but that seems really verbose and kind of sloppy.

So, my question is, is there a better way to handle this kind of situation?

Then, is it best for whatever code handles creating these menus to live in the view file, or in a helper, or in a Presenter model, or something else?

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悸初 2024-11-14 10:03:12

我最终用一个辅助方法解决了这个问题:

def browse_menu_button(name,view)
    @presenter.view == view ? btnClass = 'button selected' : btnClass = 'button'
    link_to name, browse_photos_path(:view=>view), :class => btnClass
end

然后在我看来:

#photo_browser
  = browse_menu_button( 'Recent', :recent )
  = browse_menu_button( 'Best Photography', :best )
  = browse_menu_button( 'Most Loved Placed', :loved )
  = browse_menu_button( 'Flagged', :flagged ) if user_signed_in? && current_user.has_role?(:admin)

这最终与我能想到的任何其他方法一样有效。

I ended up solving this with a helper method:

def browse_menu_button(name,view)
    @presenter.view == view ? btnClass = 'button selected' : btnClass = 'button'
    link_to name, browse_photos_path(:view=>view), :class => btnClass
end

Then in my view:

#photo_browser
  = browse_menu_button( 'Recent', :recent )
  = browse_menu_button( 'Best Photography', :best )
  = browse_menu_button( 'Most Loved Placed', :loved )
  = browse_menu_button( 'Flagged', :flagged ) if user_signed_in? && current_user.has_role?(:admin)

That ended up working about as efficiently as anything else I could come up with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文