0

我在我的应用程序中使用了 devise 和 devise_invitable。当我向用户发送邀请时,我还想为他们分配一个角色。我通过迁移向我的用户表添加了一个 role_id,并创建了一个包含允许角色的角色模型。使用 devise 和 devise_invitable 文档中的一些建议,我在我的应用程序控制器中添加了一些代码:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :role_id
    devise_parameter_sanitizer.for(:accept_invitation) << :role_id
    devise_parameter_sanitizer.for(:sign_in) << :role_id
    devise_parameter_sanitizer.for(:account_update) << :role_id
  end
end

在视图中,我有这个:

<%= simple_form_for(User.new, url: user_invitation_path,
                              html: { class: 'form-inline' }) do |f| %>
  <%= f.input :email, placeholder: 'Email', label: false %>
  <%= f.collection_select :role_id, Role.all, :id, :name, {} %>
  <%= f.button :submit, 'Invite User', class: 'btn-primary' %>
<% end %>

每次我通过输入地址并选择角色来创建新用户时,role_id 仍然为零(它没有被设置)。

如果我在 configure_permitted_pa​​rameters 的开头和结尾添加一些日志记录,则该方法内没有任何失败。但是,在它完成后,我在服务器输出中看到了这一点:Unpermitted parameter: role_id。

我错过了什么!?

4

2 回答 2

3

尝试将:role_id参数添加到邀请操作:

devise_parameter_sanitizer.for(:invite) << :role_id

https://github.com/scambra/devise_invitable#strong-parameters

于 2015-04-15T18:15:44.457 回答
0

有同样的问题并像这样解决:

如果您想在视图中设置表单发送的角色,那么您只需将其添加给您invitations_controller.rb

private
  def invite_resource
      resource_class.invite!(invite_params, current_inviter) do |invitable|
      invitable.update!(role: invite_params[:role]) #setting role
    end
  end
  def invite_params
    params.require(:user).permit(:name, :email,:invitation_token, :role) #overriding params
  end
于 2018-01-15T15:00:19.623 回答