I have a user model with a column superuser:boolean. I also using a navigationbar where I want to to have three different views according to those conditions:
When someone is not signed in
When someone is signed in as a user
When someone is signed is as a user and the boolean value for superuser is true
<% if (user_signed_in? && user.super_user?) %>
<% elsif (user_signed_in?) %>
<% else %>
I am getting the error: "undefined local variable or method `user'"
How can I check if the column super_user of an user is true or false?
1) It looks like you're using Devise gem (because of user_signed_in?). In this case it is a current_user helper you're looking for, not user.
2) You do not need ? in here current_user.super_user - column is called super_user, not super_user?.
<% if (user_signed_in? && current_user.super_user) %>
<% elsif (user_signed_in?) %>
<% else %>
Related
I want to show the user a part of the page if he has an attribute with a certain value.
something like this
<% if user.st == "Completed" %>
<p>just for him</p>
<% end %>
I get undefined method for nil class, how can I select the class in my views directly so I can access the attribute. I cannot use any params
edit: If I do this it works, but I want to check the attribute not if he is signed in
<% if user_signed_in? %>
<% end %>
thanks
In place of user use current_user, I'm assuming that it is for signed in user only
<% if current_user.st == "Completed" %>
<p>just for him</p>
<% end %>
You can do something like this:
In your controller:
#user = User.find(params[:id]) #or whatever user you want
In your view:
<% if #user.st == "Completed" %>
<p>just for him</p>
<% end %>
I am trying to validate what kind of role does the current logged in user has.
Role is a string column on the Users Model.
I am trying this:
<% if current_user.try(:role == 'admin') %>
#Show something
<% end %>
And I get the following error:
TypeError in Merchants#index
false is not a symbol
I have Devise as my authentication system, that is why I am using the current_user method
I used to have a boolean User.admin and it worked like this:
<% if current_user.try(:admin?) %>
#Show something
<% end %>
I need to change to a string field since I will have several roles.
It should be
<% if current_user.try(:role) == 'admin' %>
Currently, I have in my 'show' view for a question model
<% if current_user.id == #question.user_id %>
<%= link_to 'Edit', edit_question_path(#question) %>
<% else %>
<% end %>
To allow the user that created the question to edit it. This works fine when a user is logged in.
However, if a guest is not logged in. I get this error:
NoMethodError in Questions#show
undefined method `id' for nil:NilClass
It doesn't seem to like this line
<% if current_user.id == #question.user_id %>
Can anyone advise a rewrite to get this to work with guest users too?
Thanks
Why not do something like <% if current_user == #question.user %>? Take out the IDs.
Or if you really want the IDs. something like <% if current_user.present? && current_user.id == #question.user_id %>
In a helper
def current_user?(user)
current_user && current_user == user
end
Then in the view
<% if current_user?(#question.user) %>
You can't have an user.id if the user is not in the database. There is a good screencast about this: http://railscasts.com/episodes/393-guest-user-record?view=asciicast
#BillyChan - for that requirement, i would allow upvoting if someone isn't signed in. No need to create an unsaved user record. I'd probably want to use cookies to stop people multiple-upvoting, but to be honest i'd probably argue with the client that we shouldn't let people upvote without being logged in.
Use this
<% if current_user.present? && current_user.id == #question.user_id %>
Hi I'm having some trouble getting different views to display based on whether or not the user being viewed is the logged in user.
I'm using Rails 3.2 and Devise
I've tried the code below but it always returns the template for the logged in user (where I have edit info links) rather than the one for the other users.
<% if user = current_user %>
<%= render 'users/myprofile' %>
<% else %>
<%= render 'users/viewprofile' %>
<% end %>
Thanks very much
The statement should be..
<% if user == current_user %>
The reason being, = is for assignment, == is for comparison.
Check the sign of comparison. You have used equals to sign.
<% if user == current_user %>
= sign assigns the current user to user variable. Hence the condition will always be true unless there is no current user.
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 %>