Creating a formula in a Rails view?
I would like to create a small formula in my view/home page. Basically it should say if current user signed in (got that part right) AND USER CREATED A REGISTRY = TRUE then this or that.
Here is the code below:
<% if user_signed_in? and?????? %> <%= link_to "Show My Registry", current_user.registry %> <% else %> <%= link_to "Create a new registry", new_registry_path %> <% end %>
Thanks for any help.
Assuming Registry is a model that belongs to a User, you could use <% if user_signed_in? && current_user.registry.exists? %>
I assume you all ready have the relationship between Users and Registries established. If so your if so you could create a method in your users controller that looks something like.
def user_registry User.current.registry end
Then if your view your if statement should look something like
<% if user_signed_in? and user_registry? %> <%= link_to "Show My Registry", current_user.registry %> <% else %> <%= link_to "Create a new registry", new_registry_path %> <% end %>
You might want to nest the two decisions. What happens if the user is not signed in? Do you still want the user be able to create a registry without signing in? Other option would be to consider using a before_filter to ensure user is always signed in before she gets to this decision point in your app.