我可以在Rails的表单处理期间使用其他数据保存cookie值吗?

时间:2013-05-09 16:54:36

标签: ruby-on-rails ruby-on-rails-2

我已经在我的Rails控制器中设置了三个cookie,以获取有关访问者登陆网站时的一些信息。当他们提交表单时,我需要将这三个cookie的值与其他表单数据一起保存在数据库表中。我已经运行了迁移来添加额外的列但是我找不到任何有助于实际保存表单提交的值。

有谁知道这是否可行,如果有,我该怎么做?

def process_contact_us
@form = Form.find(1)    
@contact_submission = ContactSubmission.new(params[:contact_submission])    
if @contact_submission.save
  if @contact_submission.newsletter
    begin
      logger.info "Sending autoresponder"
      et = ExactTarget.new(ApplicationController::ET_USER,ApplicationController::ET_PASS, true)
      et.add_subscriber_with_options({
        "Email__Address" => @contact_submission.email,
        "First__Name" => @contact_submission.first_name,
        "Last__Name" => @contact_submission.last_name,
        "Address" => @contact_submission.address,
        "Address__2" => @contact_submission.address2,
        "City" => @contact_submission.city,
        "State" => @contact_submission.state,
        "Zip__Code" => @contact_submission.zip,
        "TravelNewsletter" => "Yes"
      }, ApplicationController::ET_LIST_ID)
      et.single_send(@contact_submission.email, ApplicationController::ET_EMAIL_ID, "", "")             
      logger.info "Added to ET"
    rescue
    end
  end

  Notifier.deliver_contact_us(@contact_submission, @form)

  session[:form] = nil
  redirect_to "/contact-us/thank-you"
else
  flash[:form] = @contact_submission
  flash[:notice] = @form.error_message.blank? ? "First Name, Last Name, and Email are all required fields." : @form.error_message
  redirect_to :back
end

1 个答案:

答案 0 :(得分:1)

要将信息保存到contact_submission对象,应在使用表单中的值初始化它们之后设置其值,但在保存之前:

@contact_submission = ContactSubmission.new(params[:contact_submission])
@contact_submission.super_cool_value = cookies[:super_cool_value]
if @contact_submission.save
  #.. continue on