3

I have spent hours trying to find a resource that explains the process of submitting form data directly to a session variable, but I have had no luck finding anything!

Essentially I am not wanting to store the data in the database when the user submits in this particular form, I just want it to be assigned to the session[:member_pin] variable when the user submits the form, so I can then check if the pin they entered matches the pin on the members database record.

Please let me know if you need anymore clarification for what I am trying to do, and thank you so much for your help!

4
  • Why do you need to store anything in the session at all? Just verify the pin when it is submitted… Commented Feb 13, 2012 at 18:24
  • Oh! I didn't even know you could do that... what would that action look like? Commented Feb 13, 2012 at 18:41
  • 1
    session[:verified] = User.find_by_pin(params[:pin]).present? Commented Feb 13, 2012 at 18:43
  • This isn't checking the submitted pin against the current User's pin... this is just checking whether or not that pin exists in the database under any user... Any idea how to specify this same snippet to the current user? Commented Feb 14, 2012 at 7:18

2 Answers 2

4

You don't have to save the data to database every time a form is submitted. In your controller 's action, get the params you want and store them in the session. Eg.,

def some_action
  session[:user_id] = User.find_by_pin(params[:pin]) if params[:pin]
end

Then in your application controller, make a helper method like this. Then you should be able to access "current_user" method in your views. (It will be nil if you haven't got any user verified with pins.

def current_user
  User.find(session[:user_id]) if session[:user_id].present?
end
Sign up to request clarification or add additional context in comments.

2 Comments

How would my form_for be set up?
Just updated the answer. Please check again. The form just has to submit the "pin" in a text field, I guess.
0

maybe something like this in your controller method:

session[:member_pin] = params[:member_pin_input_name]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.