Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set/use session variables in ruby on rails controller

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
like image 675
Anoop Avatar asked May 13 '14 12:05

Anoop


2 Answers

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
}
like image 161
Kimmo Lehto Avatar answered Oct 19 '22 04:10

Kimmo Lehto


Try this in config/initializers/session_store.rb

AppName::Application.config.session_store :cookie_store, key: '_app-name_session'
like image 1
user2503775 Avatar answered Oct 19 '22 03:10

user2503775