7

Can somebody tell me the right way to set/use session variables in a ruby on rails application from scratch. I am not able to set/use a session variable in my controller between two pages. I am using CookieStore session type. This is the syntax being used to set and get session variables:

session[:test] = "testing"

@test_str = session[:test]

Let me know in case I am missing on something.

This is how my controller looks like:

class PaymentsController < ApplicationController

  # GET /merchant_test  
  def merchant_test
    session[:test] = "testing"
    render :layout => false 
  end

  # POST /post_to_mobikwik
  def post_to_mobikwik
    zr = Mobikwik::Request.new(params) 
    @mobikwik_data = zr.all_params
    @test_str = session[:test]
    render :layout => false    
  end

  # POST /z_response
  def z_response
    zr = Mobikwik::Response.new(request.raw_post)  
    @checksum_check = zr.valid?
    @mobikwik_post = zr.all_params
    @statuscode = @mobikwik_post['statuscode']
    @statusmessage = @mobikwik_post['statusmessage']
    if @statuscode == "0"
       @verified = zr.verified?
    else
       @verified = false
    end
    render :layout => false
  end
18
  • 4
    Looks fine to me, what happens when you do that. What's the surrounding code. Give us a full example that doesn't work. Have you checked the documentation for session? Commented May 13, 2014 at 12:47
  • Hi Matt, I get a nil value when I try to get back the value from the session. Commented May 13, 2014 at 12:54
  • Added the controller as well. Commented May 13, 2014 at 12:55
  • 1
    You can inspect the cookie using your browser, it should contain your test entry. If it does not, your cookie could contain too much information (limited to 4K). One other reason why it could fail is that your session is reset, somehow. The information will be in the cookie, but not in the session (since it is cleared). Look for the reset_session command somewhere in a before_action/filter (e.g. it is generally done after log in to disable session fixation). Commented May 19, 2014 at 14:41
  • 1
    The error is not in the code you posted here. It's most likely in the configuration somewhere or other non-local effects. @nathanvda's suggestion is best: first inspect the cookie in your browser, it will be encrypted, but you can at least see if it's being set. This is the first step in bisecting the problem—determine whether it's the setting or the getting that's the problem. ... another random thought: if the Secure flag is set for the cookie and you are mixing http/https requests. Commented May 20, 2014 at 18:44

3 Answers 3

1

Your session cookie settings might be wrong. You should inspect the headers of the response and see what the Set-Cookie header looks like.

It might have a wrong domain or perhaps your cookie is https only and you're on http.

The configuration is usually done in some place like config/initializers/session_store.rb

Myapp::Application.config.session_store :cookie_store, {
  :key =>           '_appname_session_id',
  :path =>          '/',
  :domain =>        nil,   # accepted domain, for example '.example.com'  
  :expire_after =>  nil,   # the session will be expired in X seconds unless active
  :secure =>        false, # if true, cookie is valid only on https
  :httponly =>      true   # if true, javascript can't access the cookie
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi kimmmo, thanks for your response. I tried implementing what you sugeested but it also doen't seems to work for me. Still unable to get the session parameter stored in one controller function in same controller's another function.
1

Try this in config/initializers/session_store.rb

AppName::Application.config.session_store :cookie_store, key: '_app-name_session'

4 Comments

This is what I have in my session_store.rb file MobikwikDemo::Application.config.session_store :cookie_store, key: '_mobikwik-demo_session'
Your app is mobikwik-demo or mobikwik_demo?
mobikwik-demo is my app. Though the name of my app is MobikwikDemo. Will that make a change ?
I have code like you, but app name like- app_name with underscore, not dash. And I think this is the only change...
0

As a rule of thumb, in rails we create a method in ApplicationController, called current_user In this free RailsCast, you see exactly are you need:

1 Comment

I am not using active_record_store. I am using cookie_store session managment. As far as I understand in that there is no need to create any method. We should be able to directly add parameter to session arrary and again should be able to fetch the same from it. But its not happening in my application.

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.