charlotte-ruby / impressionist

Rails Plugin that tracks impressions and page views
MIT License
1.54k stars 311 forks source link

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

Open KNejad opened 4 years ago

KNejad commented 4 years ago

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 commented 3 years ago

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:inassociative_create_statement' impressionist (2.0.0) app/controllers/impressionist_controller.rb:27:in impressionist' app/controllers/films_controller.rb:28:inshow'

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

willywg commented 3 years ago

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

image

lusinh commented 3 years ago

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 commented 3 years ago

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

KNejad commented 3 years ago

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 commented 3 years ago

@KNejad you can try my version in #293

rzjfr commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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

KNejad commented 3 years ago

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 commented 2 years ago

Error still persists.

bobfly commented 2 years ago

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 commented 2 years ago

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

gem 'impressionist', '~> 1.6.1'
dcalixto commented 2 years ago

Same issue here with rails 6

stadniklksndr commented 1 year ago

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 commented 1 year ago

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 commented 7 months ago

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