I’m new to Rails and web development in general. I’m trying to better understand what are the options to render different buttons on my views depending on which user is visiting a page. I’m working with Rails 3.2.
2 specific cases for me:
-
On my movie page, I want to display a Bookmark button. The button should say “add bookmark” and not be checked if the user is not logged in or if the movie is not in user’s bookmark list. The button should say “remove bookmark” and be checked (with an “active” css class) if the user added the movie in her bookmarks.
-
If user search for “action movie”, the view renders a list of movies. Each item of the list should display an “add bookmark” / “remove bookmark” button depending on whether the movie is in the user’s bookmark list.
What are best practices to manage my buttons text, actions, and display depending on my user status? Where does the code go (view, javascript…), and what key methods I would need to implement?
Well, best practice is to using something like Draper: https://github.com/jcasimir/draper which handles the responsibility of presenting the correct content based on a set of conditions.
This would work however, but should probalby be refactored into a Draper Decorator:
but again, in the decorator you could just do it once
and then in your view you could just call:
Otherwise, you’ll be repeating that logic all over your views which is not good.
Obviously some of that is psuedo-code but I think you get my drift.