Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoMethodError (undefined method `cookie_value' for nil:NilClass) in API only Rails project #292

Open
KNejad opened this issue Nov 4, 2020 · 17 comments

Comments

@KNejad
Copy link

KNejad commented Nov 4, 2020

I'm getting the above error on my API only Rails project.

I think the issue is because we are running an API only Rails App without any cookies, and Impressionist is expecting to be able to retrieve the cookie_value from request.session_options[:id], which is nil for us.

Is there any way to handle this case without requiring cookies? Have I made a mistake here?

If not, would there be any interest in a PR to enable support for API only Rails projects without sessions? If so I'd be happy to implement this. Based on what I've seen it shouldn't be too hard.

This is the full stack for the impressionist part of the error:

impressionist (2.0.0) app/controllers/impressionist_controller.rb:143:in `session_hash'
impressionist (2.0.0) app/controllers/impressionist_controller.rb:60:in `associative_create_statement'
impressionist (2.0.0) app/controllers/impressionist_controller.rb:132:in `direct_create_statement'
impressionist (2.0.0) app/controllers/impressionist_controller.rb:45:in `impressionist_subapp_filter'
impressionist (2.0.0) app/controllers/impressionist_controller.rb:7:in `block in impressionist'
@aaronzomback
Copy link

Yes, I'm having the same error but in a standard Rails App. The gem was working fine in my Film model, updating the impressions correctly, but suddenly I get the undefined method 'cookie_value' for "60cb104e4befe185a8b81aac9a2c5e3c":String

It seems like it has something to do with the session_hash.

impressionist (2.0.0) app/controllers/impressionist_controller.rb:143:in session_hash' impressionist (2.0.0) app/controllers/impressionist_controller.rb:60:in associative_create_statement'
impressionist (2.0.0) app/controllers/impressionist_controller.rb:27:in impressionist' app/controllers/films_controller.rb:28:in show'

Not sure how to solve this. Would appreciate any insights.

@willywg
Copy link

willywg commented Dec 1, 2020

Same error here but only on a mobile device on a standard Rails App.

image

@lusinh
Copy link
Contributor

lusinh commented Dec 2, 2020

Me too, same error when using direct url in Incognito mode (need go to main page to resolve)
Screen Shot 2020-12-02 at 09 39 57

@rzjfr
Copy link
Contributor

rzjfr commented Dec 20, 2020

@KNejad does #294 fixe your issue?

@KNejad
Copy link
Author

KNejad commented Dec 20, 2020

Hi @rzjfr unfortunately this doesn't seem to be working for me. I installed your version of the gem and I'm getting this error:

NoMethodError (undefined method `id' for #<Hash:0x00007f06f489fe70>):
  
impressionist (9092d938ab45) app/controllers/impressionist_controller.rb:152:in `session_hash'
impressionist (9092d938ab45) app/controllers/impressionist_controller.rb:68:in `associative_create_statement'
impressionist (9092d938ab45) app/controllers/impressionist_controller.rb:140:in `direct_create_statement'
impressionist (9092d938ab45) app/controllers/impressionist_controller.rb:46:in `impressionist_subapp_filter'
impressionist (9092d938ab45) app/controllers/impressionist_controller.rb:8:in `block in impressionist'

When I print session from the controller this is what's returned:

{"warden.user.user.key"=>#<User email: "my@email.com", ...>}

@lusinh
Copy link
Contributor

lusinh commented Dec 21, 2020

@KNejad you can try my version in #293

@rzjfr
Copy link
Contributor

rzjfr commented Dec 21, 2020

@KNejad if it's possible to share your Gemfile.lock I can have a look

@lusinh oh yes sorry I didn't realize there were another version. I did that not specifically for this issue

@KNejad
Copy link
Author

KNejad commented Dec 21, 2020

Hey @rzjfr just attached the Gemfile.lock (renamed to Gemfile.lock.txt because of GitHub filetype restrictions). I had to lightly edit it to remove a couple of private gems and sources, but I didn't change much. It could just be caused by a strange configuration on our side. So if it's working for other API only apps then don't worry about closing this issue if you think that's the case.

Gemfile.lock.txt

@lusinh I tried your branch as well. Got the same error, the error seems to be happening before all the lines both of you edited

@GraemeHarrison
Copy link

@KNejad were you able to get it working? I think this might be a similar issue to what I'm experiencing here.

@KNejad
Copy link
Author

KNejad commented Feb 21, 2021

Hi @GraemeHarrison, no I don't think I ever did. We ended up doing a simple version of what impressionist does in house in the end (basically a hook in the controller followed by saving requests to the DB) since it was good enough for our use case, so I haven't used the gem since posting the issue.

@Talha345
Copy link

Error still persists.

@bobfly
Copy link

bobfly commented May 12, 2022

I had that issue when I tried to share url on facebook. Solution was to check if request is xhr. this was my solution and I don't have that issue anymore
Screenshot 2022-05-12 at 21 41 27

@masahiro04
Copy link

I had a same problem and downgraded gem version, and then solved it!

gem 'impressionist', '~> 1.6.1'

@dcalixto
Copy link

Same issue here with rails 6

@stadniklksndr
Copy link

Issue still exists
Had the same error in incognito mode and RSpec system tests

# Quick fix
def show
   ...
  if session.id.present?
    impressionist @question, nil, unique: %i[impressionable_type impressionable_id session_hash]
  end
end

@polgasull
Copy link

Hey @stadniklksndr the problem of your solution is that we lost the tracking for that users.
The error only appears on mobile.
On desktop is working like a charm.

@hechien
Copy link

hechien commented Mar 24, 2024

Me, too. I've got this error message.
Does anybody tried to fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.