设计:能够将参数传递给 Registrations#sign_up 操作

2024-02-23

我们偶尔会向潜在客户发送定制的注册链接。该链接包含可用于预填写注册表的参数。

http://www.example.com/users/sign_up?user[company_name]=Foo&user[region]=NA

我们的注册表包含接受公司名称和地区的字段。可以根据注册链接预先填写。

这在实践中应该有效,但这并不是因为registrations#new行动已实施。这new https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb#L6行动称为build_resource具有空哈希的方法。

def new
  resource = build_resource({})
  respond_with resource
end

The 构建资源 https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb#L85方法忽略了resource_params当输入非零时。

def build_resource(hash=nil)
  hash ||= resource_params || {}
  self.resource = resource_class.new_with_session(hash, session)
end  

我不得不超越new我的注册控制器中的操作来解决这个问题。我不喜欢我的解决方案,因为它很脆弱。

def new
  resource = build_resource
  respond_with resource
end

有没有理由new使用空哈希调用操作?是否可以在没有空哈希的情况下调用它(就像在create行动)?


我最终推翻了build_resource并将更改范围确定为new action.

def build_resource(hash=nil)
  # scope the change to new actions 
  return super unless action_name == "new"
  super.tap do |user|
    user.company_name = params[:user][:company_name]
    user.reg‭ion = params[:user][:reg‭ion]      
  end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

设计:能够将参数传递给 Registrations#sign_up 操作 的相关文章

随机推荐