我使用两个资源设计,管理员和用户, 管理员与tatausaha的关系以及与大师的用户关系
与tatausaha的管理关系没有错误
但是与大师的用户关系我有错误无法大量分配受保护的属性
模型user.rb 上的
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :guru_attributes
has_one :guru
accepts_nested_attributes_for :guru
end
on model guru.rb
class Guru < ActiveRecord::Base
attr_accessible :namaguru, :nip, :user_id
belongs_to :user
end
在registrationusers_controller.rb
上class RegistrationusersController < Devise::RegistrationsController
layout :layout_by_resource3
skip_before_filter :require_no_authentication
before_filter :authenticate_admin!
before_filter :resource_name
def resource_name
:user
end
def new
@user = User.new
@user.build_guru
end
def create
build_resource
if resource.save
if resource.is_a?(User)
redirect_to new_userguru_registrationuser_path, :notice => "Guru was successfully created."
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => new_user_session_path
end
else
clean_up_passwords resource
respond_with resource
end
end
end
在routes.rb
上 devise_for :users, :controllers => {:registrations => "registrationusers" }
我收到了诸如
之类的错误ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attribut
es: guru_attributes):
app/controllers/registrationusers_controller.rb:17:in `create'
如果有人问我为什么不使用cancan或角色用户进行设计?我想试试这个。 如何解决我的问题,任何人都可以帮助我吗?感谢的
答案 0 :(得分:0)
@user = User.new(params[:user])
@user.guru.update_attributes(params[:guru])
if @user.save && @user.guru.update_attributes(params[:guru])