0

I wanted to add a new Boolean attribute to my user: is_student

My project has 2 types of users: students & faculty. If is was a student is_student should be true and if it is faculty is_student would be false.

To add this new param I wrote this code in UsersController:

def user_params
  params[:user].permit(:email, :password, :is_student)
end

And I wrote this code in routes.rb to have it overwritten:

resources :users, controller: :users, only: :create

as was shown in this stack overflow example: rails 4 and thoughtbot/clearance - adding fields to the user model

This is my user.rb class to add my new attribute "is_student":

class User < ActiveRecord::Base
  include Clearance::User
    
  @is_student = true
  
  def is_student?
    return @is_student
  end
  
end

Right now, I have it set to true just to see that it works and it currently does not. I can see that it does not work because when I run my rails app I have this as my main page:

<% if signed_in? %>
  
  <% if current_user.is_student? %>
    <p> signed in and is student </p>
  <% end %>

  <p> signed in but not student </p>

<% else %>
  <p> not signed in </p>
<% end %>

From this code: I am seeing "signed in but not student" even though is_student should be defaulted to true. Can anyone tell me what I have done wrong?

Joey O'Neill
  • 124
  • 1
  • 6

1 Answers1

0

In user.rb, I just needed to replace the "@" with ":" in my is_student? method.

def is_student?
  return :is_student
end
Joey O'Neill
  • 124
  • 1
  • 6
  • This is not a solution. `is_student?` will always be "truthy" now because anything that is not `nil` or `false` will evaluate as as "truthy" value. You issue is actually that the instance variable `@is_student` was declared in the class scope rather than this instance scope. What you need to do is explain how you determine if a user is a "student" or "faculty" and use that information to create a boolean method if you need one – engineersmnky Nov 13 '20 at 21:20