Rails Devise not Deleting Account - authentication

I am starting a new web app and I am using Devise for the authentication.
I have got Devise working with register, login, edit and viewing the user profile. The only problem I am having is clicking the 'Delete Account' just redirects to /users and doesn't actually delete the user. What am I doing wrong?
The delete account button
= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete
As far as I know, I haven't changed anything that would break this. The delete account button I want to use is
= negative_trash_button_link_to "Cancel my account", registration_path(resource_name), :method => :delete, :confirm => "You sure?"
The negative_trash_button_link_to is in the css3buttons gem.
Edit: My routes.rb
resources :posts
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config
root :to => "home#index"
devise_for :users do
get "/users/sign_out" => "devise/sessions#destroy", :as => :destroy_user_session
end
resources :users, :only => :show

It would be helpful to tell us the rails version and the javascript libraries / frameworks that you have installed.
If you are using jquery or any other framework is working, test if it is working in general if is is using the appropriate helpers (for example the jquery-rails helpers in case of jquery). Most probably is a javascript issue, check also for any javascript errors.

Look your javascript console. You seems to have a javascript error which prevent the request to be sent.
Do you have the alert "Are you sure?" before the redirection ?

Related

How do I redirect devise sign_in path to a configured URL?

In a rails 3 project with devise, how puts the login page /users/sign_in in the root path.
my application is placed in
URL like: http://host/my_app
I am using devise for authentication
when i call the URL, the URL should automatically redirect to
http://host/my_app/users/sign_in
but
my application is forwarding to
http://host/users/sign_in
how to redirect the devise authentication to http://host/my_app/users/sign_in?
Thanks in advance
The devise docs talk about how to update the sign-in and sign-out routes. You should be able to prepend my_app using the same procedure documented.
you can skip default routes, check it out here
# config/routes.rb
devise_for :users, :skip => [:sessions]
as :user do
get 'my_app/users/sign_in' => 'devise/sessions#new', :as => :new_user_session
post 'my_app/users/sign_in' => 'devise/sessions#create', :as => :user_session
delete 'my_app/users/signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end

Devise, can't log out

In a Rails app, I use devise to manage my users and my link to destroy a session no longer work. It was working, and now I have add active admin, It doesn't.
My link is
<%= link_to "Déconnexion", destroy_user_session_path, :method => :delete, :class => 'button' %>
My routes.rb
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks"}
My rake routes
destroy_user_session DELETE /users/sign_out(.:format)
And it try to open the view /users/sign_out, so I have :
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with id=sign_out
Does Active_admin and Devise crash together?
It will be weird 'cause active use devise, no?
Edit:
For the next person who will have this issue, I solved it by adding the next line to /config/initializers/devise.rb.
config.sign_out_via = :get
Not exactly the finest way, but it does the job.
Posting Jeff Paquette's comment as an answer.
Update the config/initializers/active_admin.rb with:
config.logout_link_method = :delete
Please make changes in your routes.rb :-
devise_scope :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks"} do
get "/users/sign_out", :to => "devise/sessions#destroy"
end
I am also getting same problem, only this can resolve me after 1hr time wasting.
Thanks.

Update users details without a password by admin. Rails 3.x Devise

I would like to create a link to approve a users signup,
1) First solution<%= link_to "Approve", edit_user_registration_path(:id => user.id, :approved => true), :method => :put %>
However it drops back No route matches [PUT] "/users/edit"
2) Also I was thinking about an extra action that will respond to a specific route and update the user signup, say match '/users/approve_user', :controller => 'users', :action => 'approve_user', :collection => { :my_action => :put}
and in the view:
%- link_to "Approve", users_approve_user_path(:id => user.id), :method => :put
However, it gives back that Couldn't find User with ID=approve_user
Any help will be appreciated
I think you should use custom routing with devise
This is direct from devise documentation (https://github.com/plataformatec/devise), configuring routes
devise_scope :user do
get "sign_in", :to => "devise/sessions#new"
end
so in your case try something like
devise_scope :user do
post "Approve", :to => "users/approve_user"
end
and please note, by default device users controller comes under the name space 'devise'.

Rails 3, redirect from https to https, overwrite _path helper

I want to have mixed https/http site.
Moreover I want have redirects from https to http(ie. after user login successfully it should redirect to root page at http).
Gems like:
rack-ssl
rack-ssl-enforcer
works perfectly but only If you want to have entire site at https
"Mixed http/https" with only ssl at A, B, C actions and only http at D, E, F - dont work.
I checked solution from another SO thread:
Rails 3 SSL routing redirects from https to http
Almost works.
Its easy to write script which will change(on entire views) helper from "_path" to "_url".
But there is a problem with links like:
<%= link_to "model", some_model %>
<%= link_to "edit model", edit_mode_url(model) %>
...
There are many diffrent models and I use often "model" at iteration blocks, so solution based on 'rewrite' script will dont work with that.
Questions:
Is there a way to change behavior of <%= link_to 'model', model %> code to fix that? Is there a possibility to overwrite path helper(standard protocol will be http, on giver parameter - https)?
Or maybe there is a another solution which I have not found yet?
Edit:
I work with Rails 3.0.9.
If you would like to add https to a particular route
you can use this code
before_filter :redirect_to_https
def redirect_to_https
redirect_to :protocol => "https://" unless (request.ssl? || request.local?)
end
You can define the routes you would like to use with the before_filter action simply by doing the following
before_filter :redirect_to_https, :except => [:action1 , :action2]
before_filter :redirect_to_https, :only => [:action1 , :action2]
Use this gem:
https://github.com/retr0h/ssl_requirement
gem install ssl_requirement
Then to add ssl_required :new, :destroy #others actions
to your controllers.
If you use devise you have to overwrite each controller and specify all actions
devise_for :users, :controllers => { :confirmations => "confirmations", :omniauth_callbacks => "omniauth_callbacks", :passwords => "passwords", :registrations => "registrations", :sessions => "sessions", :unlocks => "unlocks" } do
# etc
end
It works with Rails 3.0.x

Trying to create a POST request but getting No route matches [GET]

I'm trying to do something similar to Railscasts #255 but I'm getting a No Route error:
In Ryan's routes.rb file:
post "versions/:id/revert" => "versions#revert", :as => "revert_version"
In in the controller where he uses the route, versions_controller.rb
link = view_context.link_to(link_name, revert_version_path(#version.next, :redo => !params[:redo]), :method => :post)
redirect_to :back, :notice => "Undid #{#version.event}. #{link}"
In my routes.rb
post "/approve/:id" => "listings#approve", :as => "listing_approve"
and view where I use my link:
<%= link_to 'Approve Content', listing_approve_path(#listing), :method => :post %>
My tests return to me a ActionController::RoutingError: No route matches [GET] "/approve/1"
If I leave the method as a GET everything works.. Using rails 3.1.0rc5. Any guidance as to what I'm doing wrong here would be very much appreciated..
EDIT: routes.rb file (the last line is set as match right now to work)
RLR::Application.routes.draw do
root :to => "home#index"
devise_for :users, :controllers => { :registrations => "registrations" }
devise_for :users
match '/user' => "layouts#index", :as => :user_root
resources :users, :only => :show
resources :layouts, :only => [:index, :show]
resources :listings
resources :features
resources :orders
match "/preview/:id" => "listings#preview", :as => "listing_preview", :via => "get"
match "/approve/:id" => "listings#approve", :as => "listing_approve"
end
Hmmmm, it looks right to my eye. The test sounds like it is generating a GET instead of a POST though, so it might be a problem with the link_to call. You've got :method => :post there, so it should be fine. http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to seems to indicate that link_to will generate some javascript to make a POST call on click (and that users with javascript disabled will get a normal GET link unless you use :href="#"), so it might be because your test engine isn't running the javascript.
You can fix this by changing it to a button that submits a hidden form, but that might not be the visual representation you want.
It might be a precedence thing - the first matching route definition in routes.rb is used, so if you have a resources route or something like that it may be matching on that first.
I got the same problem in my rails application and I solved it the same way you did by doing a via: :get on the match instead of a via: :post. I think for some reason when you send a request in the format of /something/:id it will automatically assume its a [GET] request and search for a get route. This of course will cause problems in your routes if you have it as a :POST.
If anyone has a better solution or idea as to why you cannot send a post request in the format '/something/:id' let me know please.