title: Rails 4, Rolify and Assigning Roles
body: <p>I am trying to make an app with Rails 4. I use, Devise, Rolify and Simple Form.</p>
  
  <p>My current problem is in trying to assign rolify roles to users.</p>
  
  <p>I have the following code:</p>
  
  <p>User.rb</p>
  
  <pre><code>def self.find_for_oauth(auth, signed_in_resource = nil)
      # Get the identity and user if they exist
      identity = Identity.find_for_oauth(auth)
  
      # If a signed_in_resource is provided it always overrides the existing user
      # to prevent the identity being locked with accidentally created accounts.
      # Note that this may leave zombie accounts (with no associated identity) which
      # can be cleaned up at a later date.
      user = signed_in_resource ? signed_in_resource : identity.user
  
      # p '11111'
  
      # Create the user if needed
      if user.nil?
        # p 22222
        # Get the existing user by email if the provider gives us a verified email.
        # If no verified email was provided we assign a temporary email and ask the
        # user to verify it on the next step via UsersController.finish_signup
        email_is_verified = auth.info.email &amp;&amp; (auth.info.verified || auth.info.verified_email)
        email = auth.info.email
        user = User.where(:email =&gt; email).first if email
  
        # Create the user if it's a new registration
        if user.nil?
          # p 33333
          user = User.new(
            # at least one problem with this is that each provider uses different terms to desribe first name/last name/email. See notes on linkedin above
            first_name: auth.info.first_name,
            last_name: auth.info.last_name,
            email: email,
            #username: auth.info.nickname || auth.uid,
            password: Devise.friendly_token[0,20])
  # 
          # debugger
  
          # if email_is_verified
          #   user.skip_confirmation!
          # end
          user.skip_confirmation! 
  
          user.save!
        end
      end
  
      # Associate the identity with the user if needed
      if identity.user != user
        identity.user = user
        identity.save!
      end
      user
    end
  
    def email_verified?
      self.email &amp;&amp; TEMP_EMAIL_REGEX =~ self.email
    end
  
  
  
      def full_name
          [*first_name.capitalize, last_name.capitalize].join(" ")
      end
  
  after_create :add_default_role
  
  
    def add_default_role
      add_role(:pending) if self.roles.blank?
    end
  </code></pre>
  
  <p>Role.rb</p>
  
  <pre><code>class Role &lt; ActiveRecord::Base
    has_and_belongs_to_many :users, :join_table =&gt; :users_roles
    belongs_to :resource, :polymorphic =&gt; true
  
    validates :resource_type,
              :inclusion =&gt; { :in =&gt; Rolify.resource_types },
              :allow_nil =&gt; true
  
    scopify
  end
  </code></pre>
  
  <p>Users/omniauth_callbacks_controller</p>
  
  <pre><code>def self.provides_callback_for(provider)
      class_eval %Q{
        def #{provider}
          @user = User.find_for_oauth(env["omniauth.auth"])
  
          if @user.persisted?
             sign_in_and_redirect @user,  event: :authentication
  
            set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
          else
            session["devise.#{provider}_data"] = env["omniauth.auth"]
            redirect_to new_user_registration_url
          end
        end
      }
    end
  
            # sign_in_and_redirect_user(:user, event: :authentication)
  
  
    [:twitter, :facebook, :linkedin, :google_oauth2].each do |provider|
      provides_callback_for provider
    end
  
    def after_sign_in_path_for(resource)
      if resource.email_verified?
        super resource
      else
        finish_signup_path(resource)
      end
    end 
  </code></pre>
  
  <p>Users controller</p>
  
  <pre><code>private
      def set_user
        @user = User.find(params[:id])
      end
  
      def user_params
        accessible = [ :first_name, :last_name, :email, {role_ids: []}] # extend with your own params
        accessible &lt;&lt; [ :password, :password_confirmation ] unless params[:user][:password].blank?
        accessible &lt;&lt; [:approved] if user.admin
        params.require(:user).permit(accessible)
      end
  </code></pre>
  
  <p>Users#index</p>
  
  <pre><code>&lt;% Users.each do |user| %&gt;
        &lt;div class="row"&gt;
              &lt;div class="col-xs-5"&gt;
                  &lt;%= link_to "#{user.full_name}", user %&gt;
              &lt;/div&gt;
              &lt;div class="col-xs-5"&gt;
                  &lt;%= link_to "#{user.email}", user %&gt;
              &lt;/div&gt;
              &lt;div class="col-xs-2"&gt;
                   &lt;%= link_to "edit", edit_user_path(user) %&gt;
              &lt;/div&gt;
      &lt;/div&gt; 
      &lt;% end %&gt; 
  </code></pre>
  
  <p>Users#form</p>
  
  <pre><code>&lt;%= simple_form_for(@user) do |f| %&gt;
              &lt;%= f.error_notification %&gt;
                &lt;% Role.all.each do |role| %&gt;
                &lt;div class="form-inputs"&gt;
  
                &lt;%= f.input "user[role_ids][]", role.id, collection: @user.role_ids.include?(role.id) %&gt;
                &lt;%= role.name %&gt;
  
                &lt;/div&gt;
  
                &lt;div class="form-actions"&gt;
                  &lt;%= f.button :submit, "Add role", :class =&gt; 'formsubmit' %&gt;
                &lt;/div&gt;
  </code></pre>
  
  <p>I have also tried:</p>
  
  <pre><code>&lt;%= f.association :roles %&gt;
  &lt;%= role.name %&gt;
  </code></pre>
  
  <p>in the user#form
  Migration to add roles to role table:</p>
  
  <pre><code>class AddRolesToRolifyTable &lt; ActiveRecord::Migration
    def change
  
      ['admin', # internal  admin
       'manager', # internal  manager
       'editor', # internal  web content editor
       'support', # internal  user support
       'educator', # lecturers
       'faculty_manager', #manage the business side 
       'project_manager', 
       'pending', # new people that are not yet confirmed in a role - default role assignment
  
       ].each do |role_name|
        Role.create! name: role_name
    end
  
    end
  end
  </code></pre>
  
  <p>When I save this and try to run the local host and go to users#index, I get an error that says:</p>
  
  <pre><code> Couldn't find User with 'id'=
  </code></pre>
  
  <p>This method is highlighted:</p>
  
  <pre><code> private
      def set_user
        @user = User.find(params[:id])
      end
  </code></pre>
  
  <p>I can't say I've properly understood how rolify works with devise. My console shows that I have two test users in the db, each of which has an id (so im not sure how to explore this error further). Does anyone see where I've gone wrong?</p>
  
  <p>I have adapted this setup using advice in this post:</p>
  
  <p><a href="https://stackoverflow.com/questions/33950172/defining-roles-with-rolify">Defining Roles with Rolify</a></p>