无法在rails控制器上的ruby中设置/使用会话变量

时间:2014-05-13 12:42:44

标签: ruby-on-rails ruby session

有人可以告诉我从头开始在rails应用程序的ruby中设置/使用会话变量的正确方法。我无法在两个页面之间的控制器中设置/使用会话变量。我正在使用CookieStore会话类型。这是用于设置和获取会话变量的语法:

session[:test] = "testing"

@test_str = session[:test]

如果我遗失某些事情,请告诉我。

这就是我的控制器的样子:

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

3 个答案:

答案 0 :(得分:1)

您的会话Cookie设置可能有误。您应该检查响应的标头,看看Set-Cookie标头是什么样的。

它可能有一个错误的域名,或者您的Cookie只是https而且您在http。

配置通常在某些地方完成,如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
}

答案 1 :(得分:1)

config/initializers/session_store.rb

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

答案 2 :(得分:0)

根据经验,在rails中我们创建了一个方法 在ApplicationController中,名为current_userthis免费的RailsCast中,您可以确切地看到: