views:

615

answers:

3

Let's say I have a table called positions (as in job positions). On the position show page I display all the detail about the job - awesome. At the bottom I need the prospective applicant to input their professional license # before continuing onto the next page which is the actual applicant creation form. I also need to take that license # and have it populate that field on the applicant form (again on the proceeding page).

I realize there are a couple ways to do this. Possibly the more popular option would be to store that value in the session. I am curious how to do this in the simplest manner?

My idea:

  1. Create a table specifically for license #'s.
  2. Add a small form on the position show page to create license # (with validation)
  3. Store newly created license in session - not sure what to put in which controller?
  4. On applicant creation form populate from session the license #.

This would assume applicants only have one license.

Thoughts?

Appreciate the help!

+1  A: 

Don't store this in the session! Pass that as an hidden field.

Let's say the user starts the form, then open the form again in a new window or something... then the session variable would be shared between the two forms. Other problems would occur if the cookie gets removed (session expire, user clear cache...)

This is not good. The best way is using a POST variable. GET works as well but messes up the URL

marcgg
In a normal, logical scenario, why would the user start a form then start it again in a new window? True, this could be problematic, but on a basis of UX it makes no sense. Plus, from my understanding, the form itself is to create the license number, not to pass the license number, so a hidden field would not really work.
Eric
In an app I worked on, I thought the user wouldn't... and then the user did. If you create the license number you can then inject it in the next screen or whatever and keep it here.
marcgg
Ah, yes, I see that #4 now states a second form. Yes, it would be easiest and most logical to just pass the license number as an @variable (or the license object, if you need to access more details of it's object), and then use the hidden_field_tag / hidden_field helpers to set it up.
Eric
Great suggestions! Thanks for catching that pitfall. OK still trying to wrap my head on how rails passes @variables. Is it best to create the license using <% form_for(License.new) do |f| %> and then redirect_to new_candidate_path(@license). Then use hidden_field in that form? Still learning the Rails way of doing things here... thanks again!
drpepper
mike, it would be best that, in the controller method which calls the view for here, do '@license = License.new', then in the view do 'form_for @license'. Then use the hidden_field.
Eric
yep makes total sense - I wasn't thinking clearly about the redirect - obviously everything disappears when you send that new request. Thanks Eric and marcgg!
drpepper
A: 

Seems like a good idea. As for #3, for whatever controller is called in the transition from 2 -> 4, that would be the controller where you store the session, as such:

session[:license_number] = your_license_number_information

From there, it can be called the same way (session[:license_number]) to get it.

Eric
Like I said in my answer, using a session could work, but it might lead to issues. An hidden field is safer and more solid. What if the user takes forever submiting the form and the session expires?
marcgg
A: 

The hidden field is safer for data persistence. However is not not then coded in the HTML output? That can be a great data security issue.

This is a trade-off to be considered.

Jerome