Rails Devise Signout not working - ruby-on-rails-5

I am having issues logging out of my Rails app via Devise. I have looked at several different posts about this and have been struggling for a couple days now.
couldnt-find-user-with-id-sign-out
couldnt-find-user-with-id-sign-out
are some examples that have not worked so far. I have a feeling it is javascript related but I am not sure.
routes.rb:
Rails.application.routes.draw do
resources :events
devise_for :users
resources :users
resources :articles
resources :athletes
get 'welcome/index'
get 'athletes/index'
get 'users/login'
root 'welcome#index'
end
application.html.rb
<% if user_signed_in? %>
<%= link_to 'Logout', destroy_user_session_path, :method => :delete %>
application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_bootstrap
//= require_tree .
Any advise or direction would be appreciated I am not sure where to go from here.
thanks,

I resolved this be modifying ./config/initializers/devise.rb and changing
config.sign_out_via = :get

Related

Using Parsley With Rails

I am trying to add parsley.js to a rails project to do client side form validation and can't seem to get it working.
I am using the gem for parsley for the rails asset pipline - https://github.com/mekishizufu/parsley-rails
gem "parsley-rails"
I went ahead and required it in my application.js file
//= require jquery
//= require parsley
//= require jquery_ujs
//= require twitter/bootstrap
//= require_tree .
And then I added the following to the form I wish to validate on
<%= form_for :user, :html => {:"data-validate" => 'parsley'} do |user| %>
Which generates a data attribute in the html for the form
data-validate="parsley"
Then to check for the presence of the address I have:
<%= label_tag :current_address %>
<%= text_field_tag :address, nil, :data => {:"required" => 'true'} %>
Which renders a text field with a attribute for the text field
data-required="true"
Yet when I click submit no validation occurs and a post request is made. What else do I need to do in order to get parsley working on a rails 3 form? Much appreciated!
The gem uses Parsley-js version 1.2.3, as 2.0.0 is still a release candidate. Here are the docs for that version: http://parsleyjs.github.io/Parsley-1.x/documentation.html
The attributes are a bit different - the form_for tag should look like this:
<%= form_for :user, :html => {'parsley-validate' => ''} do |user| %>
and the text field:
<%= text_field_tag :address, 'parsley-required' => '' %>
Hope this helps.
Update
The 'parsley-rails' gem is now using v2.0.
The current version is now 2.0.5 and the docs can be found here http://parsleyjs.org
Using the rails gem you need to now add data-parsley-validate to the form and directives like required to the inputs.
Inside a rails form I had luck with:
<%= form_for #post, :html => {"data-parsley-validate" => true} do |f| %>
<%= f.text_field :title, :placeholder => "Title", :required => true %>
Hope this helps someone until parsley-rails gets some better links or basic usage docs in the readme. Might be a nice PR...
I got parsley to work in our Rails application by initializing parsley with
$('#form-id').parsley({ 'data-parsley-focus': 'first' });
the 'data-parsley-focus' is an option for where parsley will send the cursor if validations fail.
I included the gem as you did in gemfile, but my application.js loads parsley after the ujs adapter
//= require jquery
//= require jquery_ujs
//= require parsley

Rails best in place gem

I followed Ryan Bates' railscast on the best in place gem but cannot get it to work properly. The default value I set before_create in the user.rb model is showing up on the page but I cannot click on it to edit; it's just showing as a static element. Although, Firebug is showing that the best_in_place javascript is being loaded in the view <head>.
application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require jquery-ui
//= require jquery.purr
//= require best_in_place
//= require_tree .
users.js.coffee
jQuery ->
$('.best_in_place').best_in_place()
user.rb
attr_accessible :email, :name, :password, :password_confirmation, :avatar, :goal
...
before_create :default_values
...
private
def default_values
self.goal ||= "Write Current Goal Here"
end
users_controller.rb
respond_to :html, :json
...
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(params[:user])
format.html {
flash[:success] = "Profile updated"
sign_in #user
redirect_to #user
}
format.json { respond_with_bip(#user) }
else
format.html { render 'edit' }
format.json { respond_with_bip(#user) }
end
end
end
users/show.html.erb
...
<%= best_in_place #user, :goal %>
In your layouts/application.html.erb .. instead of javascript_include_tag(:all) do
javascript_include_tag(:application)
Also, make sure that users.js.coffee lives in app/assets/javascript
Edit
Ensure that you have the gem specified in the Gemfile and bundle installed
gem "best_in_place"
Try this, like Said Kaldybaev suggested
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require jquery-ui
//= require jquery.purr
//= require_tree .
//= require best_in_place
then instead of coffee do this at the top of the page:
$(document).ready(function() {
$('.best_in_place').best_in_place();
})
If that doesn't work, remove //= require best_in_place and //= require jquery.purr then run
rails g best_in_place:setup
me also had the same problem, solved by moving the require best_in_place to the end of require list
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require jquery-ui
//= require jquery.purr
//= require_tree .
//= require best_in_place
This works for me:
Change
$(document).ready(function() {
/* Activating Best In Place */
jQuery(".best_in_place").best_in_place();
});
To
$(document).ready(function() {

Making rails3-jquery-autocomplete work with these Post and Tag models?

I have a Post and a Tag model (the later as an attribute called :name).
They are associated with has_and_belongs_to_many.
views/posts/_form.html.erb:
<%= f.label :tags %>
<%= autocomplete_field_tag :tags, params[:tags], autocomplete_tags_name_posts_path %>
routes.rb:
resources :posts do
get :autocomplete_tags_name, :on => :collection
end
posts_controller.rb:
class PostsController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index]
autocomplete :tags, :name
Nothing happens and there are no errors.
I'm a bit confused about whether using :tags or :tags_name. As shown in the documentation.
(they both produce errors about undefined methods and variables)
application.js:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require autocomplete-rails
Any suggestions to fix this?
Maybe this example will help you, works with act_as_taggable but it's the same idea.
How to add tagging with autocomplete to an existing model in Rails?

No route matches [GET] "/users/sign_out"

Here is my actual error: No route matches [GET] "/members/sign_out"
Since most people will use "users" I thought it would be more helpful to have that in the title. At any rate, I am essential unable to logout. I can successfully edit my member profile.
I am using devise 1.4.2 and Rails 3.1.0.rc4. Also, I have generated two separate devise models - one called "members" and the other called "admins". I was able to register and log into both of them (simultaneously) by manually navigating to the correct URL path (i.e., localhost:3000/admins/sign_in/). I created some links within my application.html.haml layout file by following this RailsCast on Devise. I am aware that it only addresses signin/signout links for "members."
If I click on the signout link I get the above error. This occurs if I manually navigate to either signout URL (i.e., localhost:3000/admins/sign_out/).
Can someone tell me why this is happening? Below are the various related files. And of course, I'm a newbie...
rake routes output:
j(film_repo)$ rake routes
new_member_session GET /members/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
member_session POST /members/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_member_session DELETE /members/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
member_password POST /members/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_member_password GET /members/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_member_password GET /members/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /members/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_member_registration GET /members/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
member_registration POST /members(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_member_registration GET /members/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_member_registration GET /members/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /members(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /members(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
new_admin_session GET /admins/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
admin_session POST /admins/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_admin_session DELETE /admins/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
admin_password POST /admins/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_admin_password GET /admins/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_admin_password GET /admins/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /admins/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_admin_registration GET /admins/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
admin_registration POST /admins(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_admin_registration GET /admins/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_admin_registration GET /admins/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /admins(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /admins(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
films GET /films(.:format) {:action=>"index", :controller=>"films"}
POST /films(.:format) {:action=>"create", :controller=>"films"}
new_film GET /films/new(.:format) {:action=>"new", :controller=>"films"}
edit_film GET /films/:id/edit(.:format) {:action=>"edit", :controller=>"films"}
film GET /films/:id(.:format) {:action=>"show", :controller=>"films"}
PUT /films/:id(.:format) {:action=>"update", :controller=>"films"}
DELETE /films/:id(.:format) {:action=>"destroy", :controller=>"films"}
root / {:controller=>"films", :action=>"index"}
routes.rb
FilmRepo::Application.routes.draw do
devise_for :members
devise_for :admins
resources :films
root :to => 'films#index'
end
admin.rb (model)
class Admin < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, and :omniauthable
devise :database_authenticatable, :registerable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
member.rb (model)
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
application.html.haml
!!!
%html
%head
%title Film Repo
= stylesheet_link_tag 'compiled/screen.css', :media => 'screen, projection'
= stylesheet_link_tag 'compiled/print.css', :media => 'print'
/[if lt IE 8]
= stylesheet_link_tag 'compiled/ie.css', :media => 'screen, projection'
= csrf_meta_tag
%body.bp
#container
#user_nav
- if member_signed_in?
Signed in as #{current_member.email}. Not you?
\#{link_to "Sign out", destroy_member_session_path}
- else
= link_to "Sign up", new_member_registration_path
or #{link_to "sign in", new_member_session_path}
- flash.each do |name, msg|
= content_tag :div, msg, :id => "flash_#{name}"
= yield
You can end a session via get by changing the devise configuration in initializers.
# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get
Just open the link and your session is removed.
I had a similar problem, but addition of the :method=> :delete didn't work.
I was able to add a new route for a the get request by commenting out the devise_for :users and adding
devise_for :users do
get '/users/sign_out' => 'devise/sessions#destroy'
end
I had a similar problem.
My view code was like this:
<%= link_to " exit", destroy_user_session_path, method: :delete %>
After adding the following change to routes.rb it worked,
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
Although I don't know the cause, the reason why you are getting that message is because in your routes you have
destroy_member_session DELETE /members/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
Which means that route is only available with the DELETE method as opposed to GET. This is a bit weird since in the docs for devise it says that it should create it as GET route (https://github.com/plataformatec/devise/blob/master/lib/devise/rails/routes.rb#L30)
With it as a DELETE route, you should be able to logout using
link_to :logout, destroy_member_session_path, :method => :delete
I just needed to add the
//= require jquery
//= require jquery_ujs
to my application.js
If you want to use :delete method for security reasons and not be dependent on jquery-ujs you can use button_to instead of link_to, like:
button_to "Log out", destroy_user_session_path, method: :delete
if using link_to you must be sure to have javascript active:
Note that if the user has JavaScript disabled, the request will fall
back to using GET.
As seen in docs
In devise.rb, change
config.sign_out_via = :delete
to
config.sign_out_via = :get
This worked for me. I went crazy with this because the default is delete and I don’t understand why.
This works, but I am not sure whether it affects other elements in the application.
We still can use :method => :delete in my code, like that
= link_to "Sign out", destroy_user_session_path,:method => :delete
The reason i think we fail to load javascript that include jquery, make sure
= javascript_include_tag "application" (haml- you can use html too)
to include jquery-ui and jquery-ujs. So if it still error, i suggest to change rails gem in GEMFILE to version 3.2.6 and call bundle update to update gems. It works for me!
= link_to "Sign out", destroy_user_session_path,:method => :delete
will NOT work instead use this,
= link_to "Sign out", destroy_user_session_path,:method => 'delete'
should do the trick or worse case add require jquery_ujs in your application.js
In Rails 6:
I just changed the link_to to button_to, and the 'sign out' works properly
<%= button_to "Sign out", destroy_user_session_path, method: :delete %>
In rails 7, you need to add data: { turbo_method: :delete" } to link_to. So the link_to would look like this
<%= link_to "Log out", destroy_user_session_path, data: { turbo_method: :delete } %>
Using Rails4, I had to use the following method:
<%= link_to "Logout", destroy_admin_session_path, method: :delete %>
Emphasis on where the colons are on method: and :delete
Maybe that will help somebody.
Upgraded from Rails 3.0 to 3.1 and found this problem.
This fixed it for me:
routes.rb:
devise_for: users
devise.rb:
config.sign_out_via = :delete
application.html.erb:
<%= javascript_include_tag "application" %>
* not :defaults
_login_items.html.erb:
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
app/assets/javascripts/application.js
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree .
and I had in javascript/ jquery.js, jquery_ujs.js from 3.0 version that I've removed.
You may have removed assets/javascripts/*
Run rails generate jquery:install --ui this will generate all the javascripts as shown below
xxxx#xxxxx:~/Projects/Rails_apps/rtest$ rails generate jquery:install --ui
remove public/javascripts/prototype.js
remove public/javascripts/effects.js
remove public/javascripts/dragdrop.js
remove public/javascripts/controls.js
copying jQuery (1.7.1)
create public/javascripts/jquery.js
create public/javascripts/jquery.min.js
copying jQuery UI (1.8.16)
create public/javascripts/jquery-ui.js
create public/javascripts/jquery-ui.min.js
copying jQuery UJS adapter (822920)
remove public/javascripts/rails.js
create public/javascripts/jquery_ujs.js
Go to your layout e.g application.html.erb and edit <%= javascript_include_tag :all %>
That worked for me :)
FWIW I have also run into this problem. Have looked into all of the suggested answers however the only one which worked was to foto open routes.rb and comment out the following line:
devise_for :users
Below that, add the following line:
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
The problem begins with rails 3.1 in assets/javascript/. Just look for application.js, and if the file doesn't exist, create a file with that name. I don't know why my file disappears or never was created on rails new app... that file is the instance for jquery.
#creamhost say,
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
but it is not correct solution for me (Rails4). I solved our problem (#Olives' answer),
link_to :logout, destroy_member_session_path, method: :delete
I am using rails version 5. I encounter this problem also. The simple fix I did was to changing the devise configuration in initializes to the default.
From :
config.sign_out_via = :get
To :
config.sign_out_via = :delete
Keep your devise.rb using the correct HTTP method:
# good
config.sign_out_via = :delete
# bad
config.sign_out_via = :get
Use button_to instead of link_to
# good
= button_to "Sign Out", destroy_user_session_path, method: :delete
# bad
= link_to "Sign Out", destroy_user_session_path, method: :delete"
If you are using bootstrap (keep it classy)
= link_to "Sign Out", destroy_user_session_path, method: :delete, class: "btn btn-default btn-sm"
Ref: github.com/heartcombo/devise/issues/4570#issuecomment-740812109
Just use the following for your sign out link:
<%= link_to "Sign out", destroy_user_session_path, method: :delete %>
//= require jquery_ujs
You are missing this line in your assets. There's no need to get /users/signout request. Put this line into JavaScript file at very top of the page.
Had the same problem and remembered it only started happening after I decided to "clean up" my Javascript files. So I ran rails generate jquery:install --ui again and this solved it for me. (You can ignore the --ui part if you don't need JQuery UI, I suppose.)
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
That works for me
I believe this error is due to devise library template not aligned with changes on rails 7,
In short, you are not sending the request in method delete as expected due to the Javascript library that does that is now missing.
you can change :
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
with
<%= link_to "Sign out", destroy_user_session_path, data: { "turbo-method": :delete }, class: "btn btn-danger ml-3" %>
And it will work again
This worked for me:
<%= link_to 'Sign out', destroy_user_session_path, data: { turbo_method: :delete } %>
It happens only on windows.. Add the following thing to your Application.html.erb file.
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end

Delete link doesn't work but button does

I have a problem very similar to this one: rails 3 - link_to to destroy not working
But delete/destroy links do not work; I simply get redirected to the show page of the object. When I make a button for delete, it all works fine. But I'd like to understand why. Does anyone know?
I seems to be related to some .js files I am using/calling.
<!-- This link doesn't work -->
<%= link_to('Delete', post, :confirm => 'Are you sure?', :method => :delete) %>-->
<!-- This button does work -->
<%= button_to "delete", post, :method=>:delete, :class=>:destroy, :confirm => 'Are you sure?' %>
Post Controller
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
UPDATE
After doing some further research it seem that everyone else having a similiar issue has included the following jquery library:
<script type="text/javascript" src="/javascripts/dragdrop.js"></script>
But I still don't know what the issue is...
LOG
Started GET "/posts/7" for 127.0.0.1 at Tue Jul 12 08:34:06 -0400 2011
Processing by PostsController#show as HTML
Parameters: {"id"=>"7"}
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = 7 LIMIT 1
SQL (0.2ms) SELECT COUNT(*) FROM "comments" WHERE ("comments".post_id = 7)
Rendered posts/show.html.erb within layouts/application (116.5ms)
HTML generated
Delete
UPDATE: I've found that removing <script type="text/javascript" src="/javascripts/jquery-1.5.1.min.js"></script> fixes my problem. But I don't understand why. Is there a known conflict between jquery1.5.1 and rails 3.0.7?
Make sure you include these in your application layout:
<%= javascript_include_tag(:defaults) %>
<%= csrf_meta_tag %>
In my case adding
<%= javascript_include_tag(:defaults) %>
did not work. However explicitly defining java script files did the trick
<%= javascript_include_tag 'prototype' %>
<%= javascript_include_tag 'application' %>
Not sure yet why :defaults tag didn't work...
Rails will automatically load jquery for you if you have jquery-rails gem loaded via your Gemfile (this is the default rails configuration). It is then loaded via:
<%= javascript_include_tag(:defaults) %>
Have a look at app/assets/javascripts/application.js for the code that tells rails to add jquery via assets:
//= require jquery
//= require jquery_ujs
By trying to load another copy of jquery via this line:
<script type="text/javascript" src="/javascripts/jquery-1.5.1.min.js"></script>
You have caused a clash of jquery instances, the effect of which will be that neither will work.