【问题标题】:Using devise_invitable with enum User roles将 devise_invitable 与枚举用户角色一起使用
【发布时间】:2019-09-17 08:34:16
【问题描述】:

1.描述核心问题

我正在实施devices_invitable gem 来邀请用户。 gem 已正确添加,因为我可以向用户发送标准 devices_invitable 电子邮件以邀请他们使用该应用程序。

我遇到的问题是,我不确定如何让管理员(用枚举标识)分配他们邀请的用户的角色(例如,他们应该输入电子邮件和角色他们邀请的用户,然后应该正确添加到我的数据库中)。

注意: 正如您在下面看到的,我目前正在尝试使用带有 cocoon gem 的嵌套表单来解决它,但我对其他方法持开放态度。

2。我目前的申请

应用程序有 2 个表,它们之间存在多对多关系:

(i) 用户,其角色属性由枚举定义。

(ii) 公园

这个想法是让管理员通过输入电子邮件+角色来邀请新用户进入公园(而不是整个应用程序)。

3.代码

user.rb

class User < ApplicationRecord
  has_many :user_parks, dependent: :destroy
  has_many :parks, :through =>  :user_parks
  enum role: [:owner, :admin, :employee, :accountant]
  after_initialize :set_default_role, :if => :new_record?

  def set_default_role
    self.role ||= :admin
  end

  devise :invitable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :invitable
end

park.rb

class Park < ApplicationRecord
  has_many :user_parks, dependent: :destroy
  has_many :users, :through => :user_parks
  accepts_nested_attributes_for :users, allow_destroy: true, reject_if: ->(attrs) { attrs['email'].blank? || attrs['role'].blank?}
end

parks_controller.rb

class ParksController < ApplicationController

def update
    @park = Park.find(params[:id])
    authorize @park
    url = Rails.application.routes.recognize_path(request.referrer)
    last_action = url[:action]
    if last_action == 'edit_users' & @park.user.last.nil?
      @user=@park.user.last.invite!(email: park_params[:user][:email])
    end
    @park = @park.update_attributes(park_params)
    redirect_to parks_path
  end

def edit_users
    @park = Park.find(params[:id])
    authorize @park
 end

 private
 def park_params
    params.require(:park).permit(:name, users_attributes: [:email, :role, :_destroy])
  end

end

edit_users.html.erb

<%= render 'users_edit_form', park: @park%>

_users_edit_form

<%= simple_form_for [@park] do |f|%>

<h1>Park</h1>
<% @park.users.each do |user| %>
  <%= user.email %>
  <% end %>

<div> 
<% f.simple_fields_for User.new, :url=> new_user_invitation_path, html: { class: 'form-inline' } do |user| %>
  <%= user.input :email %>
  <%= user.input :role, priority: [:employee], collection:[:owner, :admin, :employee, :accountant] %>
  <%= f.button :submit, 'Invite User', class: 'btn-primary' %>
<% end %>

</div> 
#TEST for standard view (sends invite but does not assign park/role)
<%# link_to "new user add", new_user_invitation_path %>


<%= f.button :submit, 'Invite User', class: 'btn-primary' %>
<% end %>

【问题讨论】:

    标签: ruby-on-rails devise rails-activerecord devise-invitable


    【解决方案1】:

    我可以看到的第一个问题是您在此行上使用了按位 &:

    if last_action == 'edit_users' & @park.user.last.nil?
    

    您通常会使用逻辑和 &amp;&amp;and

    该行的第二个问题是 @park.user 应该是 @park.users 如果它被声明为 has_many

    第三个问题是我不确定你是否需要查看request.referrer

    假设您可以收到邀请!行,那么您可能需要查看 2 个不同版本的邀请文档!

    当用户已经创建时:

    user = User.find(42)
    user.invite!(current_user)  # current user is optional to set the invited_by attribute
    

    see example in README

    当用户尚未创建时:

    User.invite!(email: 'new_user@example.com', name: 'John Doe')
    # => an invitation email will be sent to new_user@example.com
    

    see example in README

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多