Marek Fajkus
Marek Fajkus

Reputation: 588

Rails 4 sessions cookie is not set

I'm building Rails 4 application. It's mostly json api so I'm also using rails-api gem in my app. I have ApiControllers which extends ActionController::API.

Now I need to add some classic server render HTML templates. Controllers for those renders extends ActionController::Base.

I have problem, that rails are not setting cookie in browser, so my sessions are not working at all. I tried to start new Rails 4 app and sessions in it and everything works fine. I don't know why it is not working in my application. set-cookie header is missing. Any ideas?

Upvotes: 3

Views: 4591

Answers (2)

Hector Acosta
Hector Acosta

Reputation: 198

Changing config.api_only = false works fine but it is not the best solution in case you are looking to use the API version and still implement functionalities like cookies and sessions it is possible by just specifying that you want to use those in application.rb like this:

config.api_only = true

config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore

Upvotes: 6

Marek Fajkus
Marek Fajkus

Reputation: 588

Got it!

rails-api change some application configuration. I have to add this code in config/application.rb

config.api_only = false

THX to this thread: Adding cookie session store back to Rails API app

Upvotes: 2

Related Questions