【问题标题】:Adding Role dynamically through Form USing Rolify along with Devise and Cancan通过 Form 使用 Rolify 以及 Devise 和 Cancan 动态添加角色
【发布时间】:2014-05-28 03:55:55
【问题描述】:

我刚刚按照教程 "https://github.com/EppO/rolify/wiki/Tutorial" 它非常好并且工作正常。但是我的问题是我们不能在不使用 Rails 控制台的情况下通过表单添加角色。

<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)

这是我的角色.rb

类角色<:base>

has_and_belongs_to_many :users, :join_table => :users_roles

belongs_to :resource, :polymorphic => true

User.rb

类用户<:base>

belongs_to :account, :inverse_of => :users

验证 :account, :presence => true

罗列

attr_accessible :role_ids

# 包括默认设计模块。其他可用的有: # :confirmable, :lockable, :timeoutable 和 :omniauthable

设计 :database_authenticable, :registerable, :recoverable, :rememberable, :trackable, :validatable

# 为您的模型设置可访问(或受保护)的属性

attr_accessible :email, :password, :password_confirmation, :remember_me, :role_ids

# attr_accessible :title, :body

has_many :auditinits

结束

感谢任何帮助!

【问题讨论】:

    标签: ruby ruby-on-rails-3 devise cancan rolify


    【解决方案1】:

    用户表单中,为角色做一个下拉选择,

    <%= user_form.select :role,options_from_collection_for_select(Role.all,"name","name) %>
    

    用户控制器中的创建操作修改为

    @user = User.new(user_params)
    
    @user.add_role params[:user][:role]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-22
      • 2013-07-10
      • 2012-06-08
      • 2017-06-04
      • 2012-08-09
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      相关资源
      最近更新 更多