Creating a formula in a Rails view? - ruby-on-rails

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.

Related

Can I query against the DEVISE current_user helper in my application menu?

The problem is, In the menu of my app I want to check if the current user has a book. If they do I will show a link to the edit book path, if not, I will show a link to the create book path.
<% if current_user.book? %>
<% else %>
<% end %>
Yes, you should be able to access current_user from any controller. But always make sure you handle if current_user returns nil.
You can use a try or safe navigation.
<% if current_user.try(:book?) %>
<% else %>
<% end %>
Or
<% if current_user&.book? %>
<% else %>
<% end %>

How to access specific object fields?

I am building a basic bare bones social media app right now.
I have a user class and a status class.
For each status, there is a "creater" (a user object) and a "subject" (a user object that the status is about). I was able to create tags by using the acts_as_taggable_on gem. What ends up happening is when a user goes to create a post, he/she can select another user from a dropdown menu. The chosen user's id attribute is then stored.
Now I am trying to link to the chosen User's profile. This is my code for show statuses on a profile page.
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<br></br>
#link to user who's associated with the tagId
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end%>
this is the line where the above code breaks
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
Can anyone help me out with this?
Not surprised this line is failing:
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
A couple points:
It's a little cleaner to separate it onto multiple lines
I suspect your problem is because you're passing a profile_name to user_profile_path instead of an id, though I can't be certain without seeing your routes.
Try the following:
<% profile_user = User.find(status.tag_list) %>
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>

Ruby on Rails: Create record from link

how can I issue a create command from a link? I have an if/else statement that displays edit or create, but I haven't found the right way to create the record.
I had this, but then I have to refresh the page to get it to function, which I can't have:
<% if FollowUp3Week.where(subject_id: sub.subject_id).first != nil %>
<%= link_to "edit", follow_up3_week_path([FollowUp3Week.where(subject_id: sub.subject_id).first]) %>
<% else %>
<%= FollowUp3Week.create(subject_id: sub.subject_id) %>
<% end %>
And I'm trying this, but no luck so far (where subjects_path is a link to the current page):
<% if Baseline.where(subject_id: sub.subject_id).first != nil %>
<%= link_to "edit", baseline_path([Baseline.where(subject_id: sub.subject_id).first]) %>
<% else %>
<%= link_to "create", subjects_path(Baseline.create(subject_id: sub.subject_id)) %>
<% end %>
Any tips, or references I should read through, would be greatly appreciated.
I really just want the link 'create' to generate the working 'edit' link.
Thank you for your time.
You will only be able to create a record when you call a controller, so what you want to do in your case is make the link_to just render the url to the controller that have the create action, once the user clicks the link that will trigger a request to your controller which will run the create record.

Refactoring from nested ifs to link_to_if

I had the following if statement in a shared view:
<% if activity.holder.user.profile_type == "Manager" %>
DAMANAGER
<% elsif profile == "managers" %>
<%= link_to activity.holder.name, managers_enterprise_path(activity.holder) %>
<% else %>
<%= activity.holder.name %>
<% end %>
profile is a local string variable that will be "enterprises" or "managers" depending which profile is logged in!
activity is a local variable too from an #each statement
The view is shared between only two users with differents profiles (Enterprise and Manager), with this constraint:
If the managers is the owner of activity it should display DAMANAGER
When I'm seeing as manager unless the holder is the manager it should display a link to show the enterprise that owns this activity.
When I'm seeing as enterprise it should display, who owns the activity, the holder name. Without links.
So I struggled myself and refactor from that to this, that's what I got by now:
<%= link_to_if (profile == 'managers'), activity.holder.name, managers_enterprise_path(activity.holder) %>
But this doesn't display DAMANAGER and furthermore it shows the enterprise's id equal to manager's id, since it pass the manager to managers_enterprise_path. The question is:
How I can improve it?
Thanks in advance, tell me if you need more code!
I don't see a maintainable way to do it without if ... else ... end. The elsif ... else ... end part can be replaced with a link_to_if:
<% if activity.holder.user.profile_type == "Manager" %>
DAMANAGER
<% else %>
<%= link_to_if profile == "managers", activity.holder.name, managers_enterprise_path(activity.holder) %>
<% end %>

Devise Admin rails

I have followed the Devise Wiki to create a very basic admin setup by adding a admin column to my User table in a boolean format.
I have been into my table (through SQlite administrator) and assigned one of my users to be an admin.
I am then have the following code in my view:
<% if user_signed_in? %>
<% if current_user.admin? %>
<%= link_to "Admin Job Post", new_user_job_path(current_user.id) %>
<% else %>
<%= link_to "Post a new job", new_user_job_path(current_user.id) %>
<% end %>
<% else %>
<%= link_to "Post a new job", new_user_session_path %>
<% end %>
The issue I am having is that my app is only ever returning my <%= link_to "Post a new job", new_user_job_path(current_user.id) %> even when logged in with an admin user.
It would be great to get a solution on this because I have tried several variations and can't get it to work.
Thanks in advance for your help!
I'm not sure of the entirety of your code, but I have a similar setup, but I used the following to show a link once a user is logged in as an Admin (I used 'try' due to the fact that it is outside of a 'user_logged_in' check).
<% if current_user.try(:admin?) %>
<li>AdminLink</li>
<% end %>
Are you sure that you made the user an admin?
To make the user admin you should have run the following commands
User.find(#id_of_user_you_want_to_make_admin)
User.admin = true
User.save!
<% if current_user.present? && current_user.has_role?(:admin) %>
<li><%= link_to 'Some Cool Admin Feature', cool_admin_path %></li>
This is what you're looking for if you're using Rails 4 with Devise 2.x. Note the () around :admin aren't necessary, I included them for clarity here as the '?' after the method seems to throw people that are new to ruby.
Another way to give admin privileges is to simply use <% if current_user.try (:email) == "admin#example.com" %> That always works for me. That way you don't have to worry about the whole system.

Resources