我只是按照教程 ”https://github.com/EppO/rolify/wiki/Tutorial“ 它非常好,工作正常。但是我的问题是我们不能通过表单添加角色而不使用Rails控制台吗?
<%= f.fields_for :users do |user_form| %>
<div class="field"><%= user_form.label :email %><br />
<%= user_form.email_field :email %></div>
<div class="field"><%= user_form.label :password %><br />
<%= user_form.password_field :password %></div>
<div class="field"><%= user_form.label :password_confirmation %><br />
<%= user_form.password_field :password_confirmation %></div>
<div class="field">
<%= f.label :roles %>
<div class="controls">
<% Role.all.each do |role| %>
<%= check_box_tag "user[role_ids][]", role.id, @user.role_ids.include?(role.id) %>
<%= role.name %><br />
<% end %>
</div>
</div>
<% end %>
字符串
角色列连接到角色表(Rolify Roles)
这是我的role.rb
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
型
User.rb
*
class User < ActiveRecord::Base
belongs_to :account, :inverse_of => :users
validates :account, :presence => true
rolify
attr_accessible :role_ids
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :role_ids
# attr_accessible :title, :body
has_many :auditinits
end
型
任何帮助是赞赏!!
1条答案
按热度按时间q0qdq0h21#
在用户表单中,下拉选择角色为,
字符串
修改用户控制器中的创建操作为
型