Rails:如何将参数从一种形式传递给另一种 [英] Rails: how to pass params from one form to another

查看:210
本文介绍了Rails:如何将参数从一种形式传递给另一种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:我有表单,任何访问者的网站可以填写。当然,如果他/他想提交它,他们必须是一个用户。所以当他们点击提交时,他们被带到注册页面。这是因为在保存原始表单的数据之前需要一个user_id。



我想做的是从原始表单中获取参数(params [:site]),而不是在创建操作中提交它们,而是等待用户创建一个帐户。这将允许我避免将站点信息保存到没有用户标识的数据库。



我可以添加一些代码到我的网站/创建方法,像这样。 p>

  def create 
@site = Site.new(params [:site])
除非current_user?
redirect_to new_user_session_path,sites_form_data:params [:site]
end
if @ site.save
#...
end

但是,一旦有用户创建帐户,我不知道如何抓住参数[:site]?



我正在使用Devise进行这个项目,所以可能会对工程施加扳手。我想知道我是否必须覆盖原来的控制器,就像这个人一样: http://rorguide.blogspot.com/2011/04/overwrite-devise-registrations.html



我想我也可以存储数据在一个cookie。但是我不知道什么时候访问该cookie,提取数据并保存到数据库。用户创建后,绝对。但是应该是用户模型的回调?哪里应该去?



感谢任何建议或意见。

解决方案

如何在用户会话中存储:site ?这将持续跨请求,意味着您不必保留这样的手动跟踪。


Here's my situation: I've got form that any visitor to the site can fill out. Of course, if s/he wants to submit it, they have to be a user. So when they click "submit", they're taken to the sign up/in page. That's because I need a "user_id" before the data from that original form is saved.

What I would like to do is to take the params from the original form (params[:site]) and, instead of submitting them in a create action, I'd like to wait until the user has created an account. This will allow me to avoid saving the site info to the database without a user id.

I could add some code to my site/create method, like this.

def create
  @site = Site.new(params[:site])   
  unless current_user?
    redirect_to new_user_session_path, sites_form_data: params[:site]
  end
  if @site.save
  # ...
end

But I'm not sure how to grab params[:site] once I've had the user create the account?

I'm using Devise for this project, so that may throw a wrench into the works. I wonder if I'll have to overwrite the original devise controller, like this guy did: http://rorguide.blogspot.com/2011/04/overwrite-devise-registrations.html

I suppose I could also store the data in a cookie. But I don't know when I'd access that cookie, extract the data, and save to the database. After user creation, definitely. But should it be a callback on the user model? Where should it go?

I'd appreciate any suggestions or advice.

解决方案

How about just storing :site in the users session? This will persist across requests and mean that you dont have to keep such manual track of it.

这篇关于Rails:如何将参数从一种形式传递给另一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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