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

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

session[:test] = "testing" @test_str = session[:test] 

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

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

 class PaymentsController  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 

您的会话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 } 

config/initializers/session_store.rb尝试此操作

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

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