Rails 视图:创建菜单
我遇到了一个奇怪的问题。我正在显示一个视图,人们可以在其中按不同范围浏览照片,并且我的视图中有以下菜单:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终用一个辅助方法解决了这个问题:
然后在我看来:
这最终与我能想到的任何其他方法一样有效。
I ended up solving this with a helper method:
Then in my view:
That ended up working about as efficiently as anything else I could come up with.