Scoala
Scoala

Reputation: 150

check if user has attribute with certain value in views

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

Upvotes: 1

Views: 343

Answers (2)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

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 %>

Upvotes: 2

Ryan K
Ryan K

Reputation: 4053

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 %>

Upvotes: 0

Related Questions