impressionist icon indicating copy to clipboard operation
impressionist copied to clipboard

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

Open KNejad opened this issue 4 years ago • 17 comments

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'

KNejad avatar Nov 04 '20 17:11 KNejad

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.

aaronzomback avatar Nov 23 '20 13:11 aaronzomback

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

image

willywg avatar Dec 01 '20 22:12 willywg

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

lusinh avatar Dec 02 '20 02:12 lusinh

@KNejad does https://github.com/charlotte-ruby/impressionist/pull/294 fixe your issue?

rzjfr avatar Dec 20 '20 14:12 rzjfr

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: "[email protected]", ...>}

KNejad avatar Dec 20 '20 20:12 KNejad

@KNejad you can try my version in #293

lusinh avatar Dec 21 '20 06:12 lusinh

@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

rzjfr avatar Dec 21 '20 07:12 rzjfr

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

KNejad avatar Dec 21 '20 12:12 KNejad

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

GraemeHarrison avatar Feb 20 '21 14:02 GraemeHarrison

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.

KNejad avatar Feb 21 '21 10:02 KNejad

Error still persists.

Talha345 avatar May 11 '22 12:05 Talha345

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

bobfly avatar May 12 '22 19:05 bobfly

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

gem 'impressionist', '~> 1.6.1'

masahiro04 avatar Jul 05 '22 14:07 masahiro04

Same issue here with rails 6

dcalixto avatar Jul 20 '22 18:07 dcalixto

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

stadniklksndr avatar Dec 16 '22 01:12 stadniklksndr

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.

polgasull avatar Jun 01 '23 17:06 polgasull

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

hechien avatar Mar 24 '24 01:03 hechien