Rails 3:设计添加角色复选框到注册表单

我正在使用Devise进行用户注册。 我一直在阅读有关定制Devise的所有着名教程 ,但无法理解这个简单的任务。 我跟着他的模特(HABTM)

我想在Devise编辑表单中添加一个角色复选框。 我没有控制器原因Devise没有提供,但设法为新用户添加默认角色。 我能够显示选中正确信息的复选框,但无法编辑它(它不会保存任何数据)。 我需要自定义控制器吗? 如果是,怎么样? 我是HABTM关系的新手!

我的用户模型

class User < ActiveRecord::Base has_and_belongs_to_many :roles before_save :setup_role # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, :lockable and :timeoutable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me def role?(role_sym) roles.any? { |r| r.name.underscore.to_sym == role_sym } end # Default role is "User" def setup_role if self.role_ids.empty? self.role_ids = [3] end end end 

我的编辑表格(设计/注册/ edit.html.rb

  

Edit

resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>


(leave blank if you don't want to change it)


(we need your current password to confirm your changes)

Cancel my account

Unhappy? "Are you sure?", :method => :delete %>.

检查你的控制台,我得到一个’无法批量分配’错误,然后我把:role_ids放入用户模型的attr_accessible并且它有效。