rails session values disapear after current page - ruby-on-rails

rails version is 5.0.4
To simplify the question, I will use an example which I had tested on my website.
I saved some values in session in http://myhome/a
http://myhome/a only had 2 lines code.
session[:asdf] = 1
render plain: "#{session.to_hash}"
The string I got is {"session_id"=>"9913d09d270a4dd67c8782836d1aaaaa", "asdf"=>1}
After that, I turn to http://myhome/b instantly
render plain: "#{session.to_hash}"
The value I got is only {}. I cannot even got session_id from it!!
myhome is fully copied from myhome_old
I had change the production value in secrets.yml
The same code worked find on myhome_old. Therefore, I think that might not be the problem of setting of my browser.
Is there something would effect the mechanism of session of rails?

Finally, I solve this problem by deleting config/initializers/session_store.rb.
It works fine now.

Related

Integration Test Session not loading Session Cookie

Rails version: 5.1.1, upgrading from 4.2.7
I have an integration test (inherting from ActionDispatch::IntegrationTest) which hits a URL. The controller at that URL puts some stuff into the session. The test then tries to assert that certain values are now present in the session.
My session_store.rb:
Rails.application.config.session_store :cookie_store, key: '_companySite_session', expire_after: 1.year
What I'm finding is that the session variable, called from the integration test as 'session', is always an empty hash ({}).
If I look at request.host in the test, I see "www.example.com". If I look at response.cookies or cookies, I see:
#<Rack::Test::Cookie:0x00000006037108
#default_host="www.example.com",
#name="_companySite_session",
#name_value_raw=
"_companySite_session=a3RnYjRvWDlUYzRZaVlaTnlsK2RkeXBWcWFNYnQ1RVo4azNqdXNOanVWWWZVT2o5Vk0wTkVhY1dFOTBxdWNzMU5sY3c1cXhXRm0xT3JZWWZGUnluSnRuYTVoaWQ1S3ZZRXVMa1YyQ2pUMjEvOExaWkcybTdGT3lUZnZ0STRSSGVpc0dPU1VONWNwTEZxQlRvODN3eXdKZTYreVIvNDR4Y0IxMms2djlPWUZCcktYWmo1SElSZ0ttaHA5UWk5MDA5aVJzaGt3cUJ4V2tFY295ZGo4ak8ydUI3OWRnTUdkcmRpanVML3phalJtZ2c1OXByeExiRDhlcy9rR1RYUGh6NlkxV2xqNWl2VjlCMm45WXo0ajBKN0VTQ29CVGRQRVErOVIrNFZwdEZBc01oL2RaSGltUjdleGlWN3YySTdJUzcvbVlJbWdwWVNUVkw4K1pPc3JsUWxOR1BZNFVrUXFtYVVObmNlYlZrSklEbk5aU3VTck5MNm12a0NBMVF2aG5xLS1pSEJTVTVLZzJ2ZVA2M1IzTmJ4Njh3PT0%3D--f253c4e1678e53896a247b087e741cf006c5874f",
#options={"path"=>"/", "expires"=>"Fri, 08 Jun 2018 19:46:22 -0000", "HttpOnly"=>nil, "domain"=>"www.example.com"},
#value=
"a3RnYjRvWDlUYzRZaVlaTnlsK2RkeXBWcWFNYnQ1RVo4azNqdXNOanVWWWZVT2o5Vk0wTkVhY1dFOTBxdWNzMU5sY3c1cXhXRm0xT3JZWWZGUnluSnRuYTVoaWQ1S3ZZRXVMa1YyQ2pUMjEvOExaWkcybTdGT3lUZnZ0STRSSGVpc0dPU1VONWNwTEZxQlRvODN3eXdKZTYreVIvNDR4Y0IxMms2djlPWUZCcktYWmo1SElSZ0ttaHA5UWk5MDA5aVJzaGt3cUJ4V2tFY295ZGo4ak8ydUI3OWRnTUdkcmRpanVML3phalJtZ2c1OXByeExiRDhlcy9rR1RYUGh6NlkxV2xqNWl2VjlCMm45WXo0ajBKN0VTQ29CVGRQRVErOVIrNFZwdEZBc01oL2RaSGltUjdleGlWN3YySTdJUzcvbVlJbWdwWVNUVkw4K1pPc3JsUWxOR1BZNFVrUXFtYVVObmNlYlZrSklEbk5aU3VTck5MNm12a0NBMVF2aG5xLS1pSEJTVTVLZzJ2ZVA2M1IzTmJ4Njh3PT0=--f253c4e1678e53896a247b087e741cf006c5874f">
In other words, the session cookie seems to be present, and its domain seems to match the request's host. I've also used the code from How to decrypt a Rails 5 session cookie manually? to print out the value of this cookie, and the value is correct.
My test environment's config specifies config.action_controller.allow_forgery_protection = false
At this point, I'm out of ideas. The cookie is there, it seems fine, its set to the domain of the request, its not expired, but session remains an empty hash. Is it not getting updated somehow? Is it trying to update but failing silently? I've run out of things to try.
This test works fine under Rails 4.2.7.
While I haven't dived into it as fully, I've also been finding that tests' flash variable always seems to be empty, too. Again, tests that worked fine in Rails 4 are breaking because the flash always seems to be empty. I assume the flash and the session are related, so maybe the same problem affects them both.
The solution was that we had this setting in our test.rb environment file:
config.allow_concurrency = false
For some reason, after making a request in the test, this setting causes the session to be replaced with an empty hash. Rails' docs suggest that you not set this flag directly: http://guides.rubyonrails.org/v3.2/configuring.html.
I think this is something we had set in Rails 2, then got carried into Rails 4 when we upgraded, and now breaks in Rails 5.
I've logged it as an issue in the Rails repo: https://github.com/rails/rails/issues/29608

Way to examine contents of Rails cache?

I'm trying to debug stale entries in a cached view in a Rails (5.0.0.beta2) running on Heroku. I'd like to look at the entries in the cache to confirm that they are named the way that I expect and are getting expired when they should.
Is there any way to do this? I found this question, HOW do i see content of rails cache, which suggests Rails.cache.read("your_key"). So, using bin/rails c (on Heroku) I tried:
Rails.cache.read(User.find(19).cache_key) => nil
Where 19 is the :id of one of the users for whom I'm seeing stale data. This has me kind of stumped...
If I try:
User.find(19).cache_key => "users/19-20160316151228266421"
But when a cache entry is supposedly expired the log line looks like:
Expire fragment views/users/19-20160316151228266421 (0.2ms)
So I tried doing a Rails.cache.read on that path, this also returned nil – I also tried doing the same with a user that had not be expired, and got nil again.
I'm wondering if that difference in path signals a problem, or if there is a way to see the path of the key that is created (I've been assuming that it matches at least the part after the slash).
Cache has the following instance variables:
[:#options, :#data, :#key_access, :#max_size, :#max_prune_time, :#cache_size, :#monitor, :#pruning]
You can examine the data with:
Rails.cache.instance_variable_get(:#data)

Rails model: saving works locally but not on Heroku

I have a Rails model for a Recipe.
It has an attribute, views, which counts how many times it has been viewed.
In the show action of my controller, I fetch the model object normally:
#recipe = Recipe.find(params[:id])
Then I increase the view count and save:
#recipe.views = #recipe.views + 1
#recipe.save
This works without a hitch locally, but on Heroku, the save apparently doesn't happen.
No errors are thrown.
I can run this exact same code in the rails console on Heroku, and then it works.
The only way I can get it to work, is setting
config.cache_classes = false
in environmenst/production.rb
This is obvously not what I want, but I'm stumped about how to go from here.
I'm running Rails 3.2.8, Ruby 1.9.3 on the Cedar stack, using Postgresql 9.1 in both development and on production.
FWIW to future searchers looking for their own solution, Benjamin Tan's answer (in comments) of heroku restart was what worked for me when I had a similar problem.
Copying the answer from the edited question body in order to remove this question from the "Unanswered" filter:
UPDATE: Fixed
Turns out I had a file with another older definition of the controller in the app/controllers directory.
~ answer per Azzar

Trouble on retrieving a constant's value stated in an initializer

I am using Ruby on Rails 3.1 and I would like to understand why a constant's value stated in an initializer file sometimes is not retrieved as expected.
That is, in my ROOT_RAILS/config/initializers/initializer_name.rb file I have "simply" the following code:
CONSTANT_NAME = [
'value_one',
'value_two',
'value_three'
]
In a my view_file_name.js.erb file I have "simply" the following code:
<% logger.debug "#{CONSTANT_NAME.inspect}" %>
When I perform a HTTP request so to "trigger"/"run" the view_file_name.js.erb source code and then I go to check the log file, sometimes the CONSTANT_NAME is outputted, sometimes it isn't (in the latter case it outputs a [] value). I tried to restart the server many times but, after a while, the constant outputted to the log became [].
What is the problem? How can I solve that?
P.S.: I noted that the issue occurs mostly when I reload the page. This is a strange behavior and maybe there is something really subtle in my poor code that I can not fix. I think that the issue is related to the "process" to retrieve the CONSTANT_NAME value from the initializer file...

Problem with Ruby OmniComplete in Vim 7.3 finding matches at one line but not 2 lines down

Im trying to figure out why ruby omnicompl only works sometimes for me.
Here it's working as expected.
But when I try the same thing on the same ivar 2 lines down I get "Pattern not found"
Both are done the same way, typing out #current_user_session.fiCtrl+X+O
I checked tpopes rails.vim github page for open/closed issues and tried to google it without luck.
My macvim and vim is compiled with +ruby
:echo &omnifunc returns rubycomplete#Complete
:Rails! returns rails.vim 4.3 (Rails-controller)
I have my complete vimdir on github for reference.
one would imagine that it's because in img2 it's now below the setting of the variable (#current_user_session = UserSession.find).
which means that as this is now an instance it's looking for instance methods, whereas before it was returning the class method.
e.g.
User.find # => fine
user = User.find
user.find # => Method not found
to demo the difference run these:
User.methods.sort
User.find.methods.sort
you'll see that it's quite different. put bluntly you're trying to look up 'find' for a user you have. "'tom'.find" doesn't make any sense.

Resources