lolthai
lolthai

Reputation: 59

Can I save cookie values with other data during form process in Rails?

I have set three cookies in my Rails controller for some information about the visitor when they land on the site. When they submit a form, I need the values of these three cookies to be saved in the database table along with the other form data. I have run the migrations to add the extra columns but I'm not finding anything that's helpful in actually saving the values on form submission.

Does anyone know if this is possible to do and, if so, how would I do this?

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

end

Upvotes: 0

Views: 1140

Answers (1)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

To save information to the contact_submission object, you should set its values after you initialize them with values from the form, but before you save it:

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

Upvotes: 1

Related Questions