briankulp
briankulp

Reputation: 186

Ruby on Rails - Submitting Form Data Directly to a Session Variable

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!

Upvotes: 3

Views: 2501

Answers (2)

Phyo Wai Win
Phyo Wai Win

Reputation: 1160

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

Upvotes: 4

Justas L.
Justas L.

Reputation: 1782

maybe something like this in your controller method:

session[:member_pin] = params[:member_pin_input_name]

Upvotes: 0

Related Questions