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.
Related
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 %>
I want to display the page if the page is public. If the user of the page is trying to view the page if it's private and if it's the admin trying to view the page.
I have written
<% if #user.is_public == true || session[:user_id] == #user.id %>
<%= render "public_page" %>
<% else %>
Private Page
<%end%>
I don't know how to add something like below to the mix. Any ideas?
<% unless session[:site_admin] %>
The purpose is to allow the site admin to view all private pages.
Just add the check to the if:
<% if #user.is_public == true || session[:user_id] == #user.id || session[:site_admin] %>
<%= render "public_page" %>
<% else %>
Private Page
<%end%>
Btw, the more ruby way of naming is_public method would be public?
Btw btw, if your project is big and you want solid permission management it's a good practice to use Devise gem combined with CanCanCan. You then define all permissions in a single place, models/ability.rb and check permissions with can? :read, #post.
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 %>
I have links to the show pages for each game in my project and if the
games user_id matches the id of the currently signed in user then I want
it to display the edit button if they are not then it shouldn't display.
I currently have the following code set but it doesn't work. Every game
has the edit button display. The code is as followed:
<% if current_user.id = #game.user_id %>
<div id="text3"><%= link_to 'Edit', edit_game_path(#game) %></div><br />
<% end %>
Any ideas?
MrDanA's answer is most probably the error, but you may want to make this code better. Checking like that is not the Rails way of doing it. Instead, make a User instance method like :
def has_game?(game)
self.games.exists?(:id => game.id)
end
and then in your view :
<% if current_user.has_game?(#game) %> ...
(can even be better by further delegating exists into the game model, as a scope or so, if you like)
You want ==
So:
<% if current_user.id == #game.user_id %>
I'm just starting out with Ruby and Rails, trying out Devise with Rails 3. I've got a loop around a list of Posts, each of which has an associated user. I only want to display editing controls for those posts which are associated with the current user.
<% #posts.each do |post| %>
<%= link_to "show" %>
<% if current_user = post.user %>
<%= link_to "edit" %>
<% end %>
<% end %>
(The above is simplified, and from memory, so I'm sure the syntax isn't entirely right - but you get the gist.)
If no user is logged in, the posts show as intended - there's a Show link, but no Edit link. However, if I am logged in at all, all of the Edit links show up, even fir posts created by a different user.
I've verified in the console that User.find(1) != User.find(2), but for some reason the current_user = post.user evaluates to true no matter who is currently logged in. Is this to do with current_user being a helper as opposed to a "real" user object? How can I use current_user to get at the ACTUAL current user to make my comparison?
Thanks,
Dan
You're assigning rather than testing - use == - i.e.
<% if current_user == post.user %>