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.

Answers


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.


Need Your Help

How to calculate distance between lat/long in c#

c# sql-server google-maps

In a c# winform application I've a latitude/longitude List. I can to calculate in sql server (2005) query but I dont know if I can do this in c#.

How to run a compiled CUDA code on a machine that doesn't have the CUDA toolkit installed?

c++ visual-studio-2010 cuda gpu gpgpu

will any memory bound application benefit from high memory throughput of tesla(cc2.0) more than high number of cuda cores of geforce (cc5.0)?