设计:能够将参数传递给registrations#sign_up操作 [英] Devise: Ability to pass parameters to the registrations#sign_up action

查看:36
本文介绍了设计:能够将参数传递给registrations#sign_up操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,我们会将定制的注册链接发送给潜在客户。链接包含的参数可以用来预先填写注册表格。

Once in a while we send customized registration links to our leads. The link contains parameters than can be used to pre-fill the registration form.

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

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

Our registration form has fields for accepting company name and region. Which can be pre-filled based on the registration link.

这应该在实践中有效,但这不是由于注册方式# new 操作已实施。 操作将调用<$带有空哈希的c $ c> build_resource 方法。

This should work in practice, but it doesn't due to how the registrations#new action is implemented. The new action calls the build_resource method with an empty hash.

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

build_resource 方法将忽略 resource_params 当输入为非零时。

The build_resource method ignores the resource_params when the input is non nil.

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

我必须重写我的注册控制员中的 new 操作以解决此问题。我不喜欢我的解决方案,因为它很脆弱。

I had to over-ride the the new action in my registrations controller to overcome this issue. I don't like my solution as it is brittle.

def new
  resource = build_resource
  respond_with resource
end

是否有新的原因? code>动作被调用为空哈希?可以在没有空散列的情况下调用它吗(例如在 create 操作中)?

推荐答案

我最终改写了 build_resource 并确定对 new 操作的更改。

I ended up overriding build_resource and scoping the change to 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

这篇关于设计:能够将参数传递给registrations#sign_up操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆