我们偶尔会向潜在客户发送定制的注册链接。该链接包含可用于预填写注册表的参数。
http://www.example.com/users/sign_up?user[company_name]=Foo&user[region]=NA
我们的注册表包含接受公司名称和地区的字段。可以根据注册链接预先填写。
这应该在实践中起作用,但这不是因为
registrations#new
行动得到落实。 new Action 调用build_resource
带有空散列的方法。def new
resource = build_resource({})
respond_with resource
end
build_resource方法忽略
resource_params
当输入非零时。def build_resource(hash=nil)
hash ||= resource_params || {}
self.resource = resource_class.new_with_session(hash, session)
end
我不得不超越
new
在我的注册 Controller 中采取行动来解决这个问题。我不喜欢我的解决方案,因为它很脆弱。def new
resource = build_resource
respond_with resource
end
new
有什么原因使用空哈希调用操作?可以在没有空哈希的情况下调用它吗(就像在 create
操作中一样)?
最佳答案
我最终覆盖了 build_resource
并将更改范围限定为 new
行动。
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.region = params[:user][:region]
end
end
关于ruby-on-rails - 设计:能够将参数传递给注册#sign_up 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13143931/