0
votes

I used declarative_authorization for my app and had problem with creating new user.

my User model code:

  class User < ActiveRecord::Base
    ROLE_TYPES = ["admin", "user", "guest"]    
    validates_inclusion_of :roles, :in => ROLE_TYPES

    def role_symbols
       @role_symbols ||= (roles || []).map{|r| r.to_sym} 
    end

my view code:

   <% form_for(@user) do |f| %>
      ...
      <p>
         <%= f.label :roles %><br />
         <%= f.select :roles, User::ROLE_TYPES, :prompt => "Select a role" %>
      </p>
      <%= f.submit 'Add User' %>
   <% end %>

every time i tried to create a new user and select the role from the drop-down list, the view complaint:

Roles is not included in the list

from the output of the script/server, i can see the roles was actually set:

"user"=>{"name"=>"kc", "password_confirmation"=>"kc", "roles"=>"guest", "password"=>"kc", "email"=>"[email protected]"}

can anyone tell me what's wrong? why the validation wont' pass?

1

1 Answers

0
votes

Is it possible that you've got attr_accessible attributes on the user to prevent mass assignment of certain attributes and that :roles isn't in there? You would get a warning about this in your logs though. The default User class generated by restful_authentication does include the attr_accessible call so it may be there without you having added it if you are using that authentication plugin too.

Is there definitely a roles attribute of the right type for users? It looks like you're expecting roles to be a single string from your form but in the code from declarative_authorization you've got (roles || []).map which suggests that that part of the code at least is expecting an array of roles.