jpdvi
jpdvi

Reputation: 47

Ruby on Rails - Session variable on form submit

I have an application which requires visitors to fill out a form and then it redirects to a second page. The client does not want to require visitors sign up to view this page, only to fill out the form.

The way I am attempting to do this is by creating a session variable when the page is visited and then checking to see if the variable exists before the next page is accessible. Is it possible to delay the creation of the session variable until the submit action is processed? If so what would that look like?

Also, can anyone think of a better way to do this? Sorry, this is probably a dumb question.

Upvotes: 3

Views: 1847

Answers (1)

Richard Peck
Richard Peck

Reputation: 76784

The session cookie would be declared after the first submit.

I presume the first submit will load up a controller#action in which you'll then redirect to the next page. Just set the session in there:

#app/views/forms/1.html.erb
<%= form_tag form_1_submit_path do %>
  ...
<% end %>

This will allow you to do the following:

#app/controllers/forms_controller.rb
class FormsController < ApplicationController
   def form_1_submit
      session[:value] = params[:value]
      redirect_to form_2
   end
end

Thus you'll have session[:value] all set and ready to use on the next form:

#app/views/forms/2.html.erb
<%= form_tag .... do %>
   <%= text_field_tag :test, value: session[:value] %>
<% end %>

Upvotes: 2

Related Questions