1

I created the app with the help of rails composer. Using devise for authentication and cancan for managing roles. So I have 3 roles by default: Admin, User and VIP. I deleted VIP, because I don't need it. Run rake db:seed to create a default admin. Then I'm coming to localhost and seeing the "First User" as admin. I logout and register(signup) a new user. Then, signing in again as admin. I see, that by deafault, this new user doesn't have any role. And also I see, that I can change it("Change role" - button). I push it and as admin can choose whether new user will be the second admin or just User. I choose, for example, User, push "change role" and have an "ArgumentError in UsersController#update wrong number of arguments (2 for 1)".

Sooo, I have two questions: 1. How to give my admin the ability to change roles without errors. 2. How to make new signing up users to have default role "User".

Thanks!

Ok, I managed to set the default role this way:

 after_create :assign_reader_role
private
 def assign_reader_role
  self.add_role "user"
end

Here is my UserControlle:

class UsersController < ApplicationController
  before_filter :authenticate_user!


  def index
    authorize! :index, @user, :message => 'Not authorized as an administrator.'
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end

  def update
    authorize! :update, @user, :message => 'Not authorized as an administrator.'
    user = User.find(params[:id])  
   if user.update_attributes(user_params)
     redirect_to users_path, :notice => "User updated."
   else
     redirect_to users_path, :alert => "Unable to update user."
   end
  end

  def destroy
    authorize! :destroy, @user, :message => 'Not authorized as an administrator.'
    user = User.find(params[:id])
   unless user == current_user
    user.destroy
    redirect_to users_path, :notice => "User deleted."
   else
    redirect_to users_path, :notice => "Can't delete yourself."
    end
   end
    private
    def user_params
      params.require(:user).permit(:name, :email)
    end
   end

Here is models. User: class User < ActiveRecord::Base after_create :assign_reader_role rolify devise :database_authenticatable, :registerable,#:confirmable, :recoverable, :rememberable, :trackable, :validatable

  validates_presence_of :name

 private
 def assign_reader_role
    self.add_role "user"
  end
 end

Role:

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users, :join_table => :users_roles
 belongs_to :resource, :polymorphic => true

  scopify
end

UserController I've already put! And where can I take params from the form?

aprok
  • 1,147
  • 11
  • 25
  • Oh, forgot to say, "Delete"-button works perfect. It deletes the user without any errors. – aprok Sep 11 '13 at 09:02
  • add params which comes from the form and write line where the error is raised – gotva Sep 11 '13 at 10:07
  • Line 17. if @user.update_attributes(params[:user], :as => :admin) Whereas I'm a little bit newbie in rails, could you please tell me, where can I see the params, which come from the form – aprok Sep 11 '13 at 10:12
  • have you setup attr_accessible in model `User`? – gotva Sep 11 '13 at 10:21
  • in rails 4 it is made different way, in controller. So you can tell me what I should set up and I'll do that. – aprok Sep 11 '13 at 10:33
  • Ups... I thought it is 3rd Rails :) You should read about strong_parameters (https://github.com/rails/strong_parameters) and remove `:as => :admin` from update_attributes – gotva Sep 11 '13 at 10:40
  • OK, I made it!! For now I don't see the error anymore! But nothing is changing after choosing the other role and pushing the button "Change role". Any ideas? – aprok Sep 11 '13 at 10:46
  • It is necessary to know how `User`, `Role`, and user form are implemented. I can suppose that params are build incorrectly. It is necessary to check and try – gotva Sep 11 '13 at 10:53
  • You mean User and Role models? And where can I find user form? If you need this, I'll put it here. – aprok Sep 11 '13 at 10:57
  • 1. Models `User`, `Role`. 2. Controller code 3. params which comes from form – gotva Sep 11 '13 at 11:04
  • params can be taken from the server log – gotva Sep 11 '13 at 11:47
  • this is my case!! Maybe it will be more clear what to do!! http://stackoverflow.com/questions/18034494/wrong-number-of-arguments-2-for-1-rails-4-maybe-strong-params-issue/18760728#18760728 – aprok Sep 12 '13 at 12:12
  • 1
    (I did not notice user_params yesterday) I think you missed role_ids in this code `params.require(:user).permit(:name, :email)` – gotva Sep 12 '13 at 12:24
  • this can help to understand what is going in strong_parameters http://blog.sensible.io/2013/08/17/strong-parameters-by-example.html – gotva Sep 12 '13 at 12:50
  • gotva, you can answer the question and I will accept it! – aprok Dec 24 '13 at 20:20

1 Answers1

1

I think you missed role_ids in permit

def user_params
  params.require(:user).permit(:name, :email, :role_ids)
end
gotva
  • 5,919
  • 2
  • 25
  • 35