veryrarecandy
veryrarecandy

Reputation: 333

Sign Out working locally, but not working on Heroku

So I'm using devise for login/logout. I noticed that my sign_out link is working just fine locally, but won't work on Heroku even after I've added, committed and pushed.

Here is what I see in my Heroku logs

2014-04-23T22:16:09.987029+00:00 heroku[router]: at=info method=GET path=/users/sign_out host=peaceful-atoll-4795.herokuapp.com request_id=16559a9f-0cff-4179-8aeb-d393ae44de38 fwd="108.233.86.201" dyno=web.1 connect=60ms service=34ms status=404 bytes=1616

Note that it tries to use the GET method to sign out when it should be DELETE. Why is it using the GET method when my routes are correct?

rake routes:

              Prefix Verb   URI Pattern                       Controller#Action
    new_user_session GET    /users/sign_in(.:format)          devise/sessions#new
        user_session POST   /users/sign_in(.:format)          devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)         devise/sessions#destroy
       user_password POST   /users/password(.:format)         devise/passwords#create
   new_user_password GET    /users/password/new(.:format)     devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format)    devise/passwords#edit
                     PATCH  /users/password(.:format)         devise/passwords#update
                     PUT    /users/password(.:format)         devise/passwords#update

devise.rb setup as such...

config.sign_out_via = :delete

Link code from my view

<% if user_signed_in? %>
  Logged in as <strong><%= current_user.email %></strong>.
  <%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
  <%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link'  %>
<% else %>
  <%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link'  %> |
  <%= link_to "Login", new_user_session_path, :class => 'navbar-link'  %>
<% end %>

Thanks ahead of time for any of your resources or input!

Upvotes: 1

Views: 2029

Answers (4)

Nyein
Nyein

Reputation: 286

For the quick fix,

config.sign_out_via = :delete

to

config.sign_out_via = :get

in config/devise.rb.
Sadly, it works well in production mode. But it will rise an error in development mode.

here is the original link to the solution.
Ruby on Rails: Unable to sign out of application in production

Upvotes: 1

psparrow
psparrow

Reputation: 9948

The solution that worked for me using Rails 5 on Heroku was to simply re-order the js files as follows:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets

Upvotes: 1

felipero
felipero

Reputation: 462

Blind answer here, since I stil can't comment on stackoverflow. :P Would be good to see the applicaiton.js file to make sure.

Rails >~ 4.0

You may need to include the rails_12_factor gem into your gemfile. That is because rails 4 won't serve static assets by default, so there could be a problem there. Check it here: https://github.com/heroku/rails_12factor#rails-4-serve-static-assets

Rails >~ 4.1

You need to declare the precompile of assets into the application.rb or into and assets.rb initializer. This is needed to make the development mode to match the production mode. Sprockets change. Check this: https://github.com/rails/sprockets-rails/pull/84

Upvotes: 1

veryrarecandy
veryrarecandy

Reputation: 333

So, as expected my routes were correct, but my app was not able to access jquery_ujs.

jquery_ujs helps in making method: :delete work.

In order to provide the correct path to jquery_ujs I added gem 'rails_12factor', group: :production to my Gemfile and ran bundle install

rails_12factor also helps in serving static assets in Heroku.

Thanks to @MichaelSzyndel for this helpful resource...

Rails 4 Asset Pipeline on Heroku: https://devcenter.heroku.com/articles/rails-4-asset-pipeline

Upvotes: 3

Related Questions