Hi,
(Using Rails 4.0.1, Ruby 1.9.3, latest devise and cancan gems. sqlite db
for local development)
I am a rookie, setting up website and was adding roles(using cancan gem)
to
my users table. Everything works great, except when I select a role for
a
user it is not getting saved. The user gets saved/created OK but it
never
updates/ remembers any roles assigned to the user.
I was following the advice given here(Many roles per user). Any help or
advice is most appreciated…
Here is my users form…
<%= form_for(@user) do |f| %>
<%= f.text_field :email %>
<%= f.password_field :password %>
<%= f.password_field :password_confirmation %>
<% end %> <%= hidden_field_tag "user[roles][]", "" %>
/app/model/user.rb
class User < ActiveRecord::Base
ROLES = %w[admin blog_author]
def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r)
}.inject(0, :+)
end
def roles
ROLES.reject do |r|
((roles_mask.to_i || 0) & 2**ROLES.index(r)).zero?
end
end
def is?(role)
roles.include?(role.to_s)
end
Include default devise modules. Others available are:
:confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable
end