我正在使用rolify+activeadmin宝石。我有两个资源:人员和用户(默认的设计表)。Staff是一个映射只读表的模型,所以我不能在staffs表中写入。我正在使用active admin尝试使用has_one和belongs_to关联为用户添加一个角色:
class User < ActiveRecord::Base
rolify
belongs_to :staff
end
class Staff < ActiveRecord::Base
has_one :user
end在app/admin/star.rb类中,我有以下内容:
form do |f|
f.inputs "Add role" do |staff|
f.input :roles, :as => :select, :collection => Role.global
end
f.actions
end
So i want to add a role for a user using Staff admin resource.
when i click on submit form button i have this error:
NoMethodError in Admin/staffs#edit
Showing app/views/active_admin/resource/edit.html.arb where line #1 raised:
undefined method `roles' for #<Staff:0x00000005c6af70>
Extracted source (around line #1):
1: insert_tag renderer_for(:edit)发布于 2014-01-05 20:44:37
角色是用户模型的一部分,而不是人员模型。将表单添加到app/admin/user.rb中,然后您将能够为用户分配一个角色。此外,在用户的表单中,您可以分配员工记录。下面是一个示例表单:
# app/admin/user.rb
form do |f|
f.inputs 'Name' do
f.input :name
end
f.inputs 'Add role'
f.input :roles, :as => :select, :collection => Role.global
end
f.inputs 'Staff' do
f.input :staff
end
f.actions
end 还可以向staff添加委托,以便能够在Staff模型中以本地方式读取角色。
# app/models/staff.rb
class Staff < ActiveRecord::Base
attr_accessible :name, :user_id
has_one :user
delegate :roles, :to => :user
endhttps://stackoverflow.com/questions/20585275
复制相似问题