7

尝试将嵌套的自定义属性Profile(Mongoid 文档)添加到我的设计User类。当提交设计注册表单时,它应该同时创建一个用户和一个相应的配置文件对象。

我希望最终结果在我的 MongoDB 中看起来像这样:

用户:

{
  # Devise fields:
  "email": "my@email.com",
  ...
  # Custom field
  "profile" : "<object_id>"
}

轮廓:

{
  "first_name": "Dave",
  ....
}

不幸的是,每当我提交注册时,我都会在控制台中收到此信息。它成功创建了用户,但未能创建关联的配置文件。

Started POST "/" for 127.0.0.1 at 2013-04-20 23:37:10 -0400
Processing by Users::RegistrationsController#create as HTML
Parameters:
   {"utf8"=>"✓",
   "authenticity_token"=>"awN2GU8EYEfisU0",
   "user"=>
       {"profile_attributes"=>
           {"first_name"=>"Dave",
           "birthday(2i)"=>"4",
           "birthday(3i)"=>"21",
           "birthday(1i)"=>"1933",
           "occupation_title"=>"Software Developer"},
        "password"=>"[FILTERED]",
        "password_confirmation"=>"[FILTERED]",
        "email"=>"my@email.com"}}
Unpermitted parameters: profile_attributes

我有设置:

  • Rails 4.0.0beta1,Ruby 2.0.0-p0
  • 设计('rails4' 分支),Mongoid(来自 git)
  • 自定义设计注册控制器,用于添加强参数的定义。

模型/用户.rb:

class User
  include Mongoid::Document

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,
     :token_authenticatable, :confirmable, :lockable, :timeoutable

  field :email,              type: String, default: ''

  ...

  has_one :profile
  accepts_nested_attributes_for :profile
end

模型/profile.rb:

class Profile
  include Mongoid::Document
  include Mongoid::Timestamps

  # Attributes
  # ----------
  field :slug,                type: String, default: '' # Acts as user-'friendlier' slug
  field :birthday,            type: DateTime, default: DateTime.now
  field :first_name,          type: String, default: ''
  field :occupation_title,    type: String, default: ''

  belongs_to :user
  embeds_many :photos
  has_one :occupation_industry, :as => :industry
end

控制器/用户/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController

  def resource_params
    params.require(:user).permit(:email, :password, :password_confirmation, :profile_attributes)
  end
  private :resource_params
end

路线.rb

devise_for  :users,
              :path => '',
              :path_names => {
                :sign_in => 'login',
                :sign_out => 'logout',
                :sign_up => 'register'
                },
              :controllers => {
                :registrations => "users/registrations",
                :passwords => "users/passwords"
              }

我已经看过这些相关的帖子,它们似乎没有帮助:


编辑:

看起来 Devise 确实在其“rails4”分支中支持强参数(应该在几天内合并到 master 中。)查看代码,您似乎可以为设计控制器上的每个操作覆盖 params 函数。对于创建新用户,它sign_up_params而不是resource_params在我的示例中。

尽管将此名称更改为正确的名称,但它仍然不起作用......仅使用此 bang 将所有参数列入白名单似乎有效:

def sign_up_params
  params.require(:user).permit!
end

显然,这种破坏了强参数的目的......所以现在的问题是我如何允许我的嵌套属性profile_attributes(如我原来的问题所示)?

4

4 回答 4

11

我遇到了完全相同的问题,并且覆盖sign_up_params确实对我有用

def sign_up_params
   params.require(:user).permit(:email, :password, :password_confirmation, :other, :etc)
end

当然,不同之处在于我的只是标量值,而您正在尝试批量分配关系......我想这就是您应该寻找的地方。

顺便说一句,该主题中的文档仍然不存在(太新),并且代码注释建议覆盖devise_parameter_sanitizer,这不是必需的。

于 2013-04-24T15:57:20.027 回答
4

我发现了一种不同的方法,它允许所有设计的覆盖逻辑和代码驻留在应用程序控制器中。这允许为每个设计操作(登录、注册、更新)传递任何和所有自定义参数。我还为 devise_invitable 添加了一个参数清理程序,并在此处处理该逻辑(邀请,accept_invitation)。我有自定义参数,如 avatar、avatar_cache 等:

#application_controller.rb

  before_filter :configure_permitted_parameters, if: :devise_controller?

protected
  # There are just three actions in Devise that allows any set of parameters to be passed down to the model, 
  # therefore requiring sanitization. Their names and the permited parameters by default are:

  # sign_in (Devise::SessionsController#new) - Permits only the authentication keys (like email)
  # sign_up (Devise::RegistrationsController#create) - Permits authentication keys plus password and password_confirmation
  # account_update (Devise::RegistrationsController#update) - Permits authentication keys plus password, password_confirmation 
  # and current_password. More at https://github.com/plataformatec/devise#strong-parameters

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:accept_invitation) do |u|
      u.permit(:username,:validate_username, :password,:password_confirmation, :invitation_token)
    end
    devise_parameter_sanitizer.for(:invite) do |u|
      u.permit(:name,:comments)
    end

    devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:username,:password,:password_confirmation)
    end
    devise_parameter_sanitizer.for(:sign_in) do |u|
      u.permit(:username,:email,:password,:password_confirmation,:phone, :validate_username, :avatar_cache, :remove_avatar, :current_password,:remember_me)
    end

    devise_parameter_sanitizer.for(:account_update) do |u|
      u.permit(:username,:email,:password,:password_confirmation,:phone, :validate_username,:avatar, :avatar_cache, :remove_avatar, :current_password)
    end
  end

在https://github.com/plataformatec/devise#strong-parameters查找并阅读更多信息

于 2013-11-18T17:22:31.637 回答
2

我在登录时遇到了同样的问题,它说:Unpermitted parameters: password, remember_me. 并且因为我有任何继承Devise::SessionsController的控制器,所以我使用我自己的参数 sanitizer

这就是我所做的:

在 '#{Rails.root}/lib' 折叠中创建一个文件,my is hzsapa_parameter_sanitizer.rband required in config/application.rb,然后覆盖application_controller.rbdevise_parameter_sanitizer中的方法

lib/hzsapa_parameter_sanitizer.rb

class HzsapaParameterSanitizer < Devise::ParameterSanitizer
  def sign_in
    default_params.permit(auth_keys + [:password, :remember_me])
  end
end

您可以覆盖这些方法取决于您的问题:

def sign_in
  default_params.permit(auth_keys)
end

def sign_up
  default_params.permit(auth_keys + [:password, :password_confirmation])
end

def account_update
  default_params.permit(auth_keys + [:password, :password_confirmation,    :current_password])
end

配置/应用程序.rb

require "hzsapa_parameter_sanitizer"

应用程序/application_controller.rb

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

  def devise_parameter_sanitizer
    @devise_parameter_sanitizer ||= if defined?(ActionController::StrongParameters)
                                      HzsapaParameterSanitizer.new(resource_class, resource_name, params)
                                    else
                                      Devise::BaseSanitizer.new(resource_class, resource_name, params)
                                    end
  end
end

编辑:我刚刚在设计自述文件中找到了解决方案,你可以在这里关注它

于 2013-06-15T01:36:52.030 回答
1

我使用了你的代码,它对我有用!

这是我所做的

class RegistrationsController < Devise::RegistrationsController
  skip_before_filter :verify_authenticity_token, :only => :create #, :if => Proc.new { |c| c.request.format == 'application/json' }
  respond_to :json, :html, :xml

  def create
    user = User.new(devise_registrations_permitted_parameters)
    if user.save
      render :json=> user.as_json(:auth_token=>user.authentication_token, :email=>user.email,:name => user.name), :status=>201
      return
    else
      warden.custom_failure!
      render :json=> user.errors, :status=>422
    end
  end


  protected                                                            
    def devise_registrations_permitted_parameters
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end

end
于 2013-04-24T21:00:46.913 回答