ruby-on-rails – 设计:能够将参数传递给注册#sign_up动作
发布时间:2020-12-17 02:03:06 所属栏目:百科 来源:网络整理
导读:偶尔我们会向我们的潜在客户发送定制的注册链接.该链接包含的参数可用于预填充注册表单. http://www.example.com/users/sign_up?user[company_name]=Foouser[region]=NA 我们的注册表包含接受公司名称和地区的字段.哪个可以根据注册链接预先填写. 这应该在实
偶尔我们会向我们的潜在客户发送定制的注册链接.该链接包含的参数可用于预填充注册表单.
http://www.example.com/users/sign_up?user[company_name]=Foo&user[region]=NA 我们的注册表包含接受公司名称和地区的字段.哪个可以根据注册链接预先填写. 这应该在实践中起作用,但它不是由于注册#new动作的实现方式. 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 我不得不在我的注册控制器中超越新动作来克服这个问题.我不喜欢我的解决方案,因为它很脆弱. def new resource = build_resource respond_with resource end 是否有理由使用空哈希调用新操作?是否可以使用空哈希调用(如在创建操作中)? 解决方法
我最终覆盖了build_resource并将更改范围限定为新操作.
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容