设计:能够将参数传递给注册#sign_up动作
偶尔我们会向我们的潜在客户发送定制的注册链接。 该链接包含的参数可用于预填充注册表单。
http://www.example.com/users/sign_up?user[company_name]=Foo&user[region]=NA
我们的注册表包含接受公司名称和地区的字段。 哪个可以根据注册链接预先填写。
这应该在实践中起作用,但它不是由于registrations#new
动作的实现方式。 新操作使用空哈希调用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
动作来克服这个问题。 我不喜欢我的解决方案,因为它很脆弱。
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
我相信这是build_resource
方法的预期行为。 与Model.new
类似,您可以传递初始化属性的哈希值,也可以不传递任何内容,从而分别生成预填充模型和空模型。
如果你想让你的控制器动作更明确,你可以改为调用build_resource(params[:user])
,这应该避免你所关注的脆弱性。