Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging roles in rails 4
    primarykey
    data
    text
    <p>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)". </p> <p>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".</p> <p>Thanks!</p> <p>Ok, I managed to set the default role this way: </p> <pre><code> after_create :assign_reader_role private def assign_reader_role self.add_role "user" end </code></pre> <p>Here is my UserControlle:</p> <pre><code>class UsersController &lt; ApplicationController before_filter :authenticate_user! def index authorize! :index, @user, :message =&gt; 'Not authorized as an administrator.' @users = User.all end def show @user = User.find(params[:id]) end def update authorize! :update, @user, :message =&gt; 'Not authorized as an administrator.' user = User.find(params[:id]) if user.update_attributes(user_params) redirect_to users_path, :notice =&gt; "User updated." else redirect_to users_path, :alert =&gt; "Unable to update user." end end def destroy authorize! :destroy, @user, :message =&gt; 'Not authorized as an administrator.' user = User.find(params[:id]) unless user == current_user user.destroy redirect_to users_path, :notice =&gt; "User deleted." else redirect_to users_path, :notice =&gt; "Can't delete yourself." end end private def user_params params.require(:user).permit(:name, :email) end end </code></pre> <p>Here is models. User: class User &lt; ActiveRecord::Base after_create :assign_reader_role rolify devise :database_authenticatable, :registerable,#:confirmable, :recoverable, :rememberable, :trackable, :validatable</p> <pre><code> validates_presence_of :name private def assign_reader_role self.add_role "user" end end </code></pre> <p>Role: </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 scopify end </code></pre> <p>UserController I've already put! And where can I take params from the form?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload