Unable to delete resource after overriding devise registrations controller - devise

I have followed this devise how to: Redirect to a specific page on successful sign up.
I have created a new RegistrationsController
class RegistrationsController < Devise::RegistrationsController
def after_inactive_sign_up_path_for(resource)
...
end
def destroy
logger.debug 'destroy user'
...
end
end
I have changed routes.rb :
devise_for :users, :controllers => { :registrations => "registrations" } do
get 'users', :to => 'profile#index', :as => :user_root
end
and moved devise/registrations/ views under my new RegistrationsController.
With rake routes I have :
DELETE /users(.:format) {:action=>"destroy", :controller=>"registrations"}
after_inactive_sign_up_path_for is working.
But destroy action doesn't work : when I cancel my account
<%= button_to "Cancel my account", registration_path(resource_name), :confirm => "ok?", :method => :delete %>
I have the following error :
The action 'destroy' could not be found for RegistrationsController
I use Devise 1.4.5 & Rails 3.1

I just ran into the same issue. Moving the destroy method to the non private section of the controller fixed it.

Related

Overriding Devise Passwords Controller

I want to disable the
def create
self.resource = resource_class.send_reset_password_instructions(resource_params)
if successfully_sent?(resource)
respond_with({}, :location => after_sending_reset_password_instructions_path_for(resource_name))
else
respond_with(resource)
end
end
so it won't redirect at all after sending the reset password
So, I created a new file under app/controllers/users/ called passwords_controller.rb
which looks like this
class User::PasswordsController < Devise::PasswordsController
def create
self.resource = resource_class.send_reset_password_instructions(resource_params)
if successfully_sent?(resource)
flash[:notice] = "sent password"
else
respond_with(resource)
end
end
def new
super
end
def update
super
end
def edit
super
end
end
and changed in my routes to
devise_for :users, :controllers => { :invitations => 'users/invitations', :passwords => 'users/passwords' }
I also have the devise_invite gem..
When I click on a link for forgotten password I get this error
Started GET "/users/password/new" for 127.0.0.1 at 2012-11-16 10:21:07 +0200
ActionController::RoutingError (uninitialized constant Users::PasswordsController):
my rake routes are
user_password POST /users/password(.:format) users/passwords#create
new_user_password GET /users/password/new(.:format) users/passwords#new
edit_user_password GET /users/password/edit(.:format) users/passwords#edit
PUT /users/password(.:format) users/passwords#update
the link in the view is
<%= link_to "Forgot your password?", new_password_path(User) , :class => "control-group", :style => "position: absolute; bottom: 0", :id=>"forgotpass" %>
What am I missing?
The password controller is extended from devise password controller. So, extend the password controller with devise password controler.
class PasswordsController < Devise::PasswordsController
......................
end
Change the routes for password controller with devise
devise_for :users, :controllers => { :passwords => "passwords" }
and the routes will be like this :-
user_password POST /password(.:format) Passwords#create
new_user_password GET /password/new(.:format) Passwords#new
edit_user_password GET /password/edit(.:format) Passwords#edit
PUT /password(.:format) Passwords#update
If you would like to keep a namespace, try:
# routes.rb
devise_for :users, controllers: { passwords: 'users/passwords' }
# users/passwords_controller.rb
class Users::PasswordsController < Devise::PasswordsController
...
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.

Links to resources with friendly URLs

I'm trying to write a blog in Rails 3, so I have posts. I want to make nice routes for the posts like this: posts/year/month/day/post-title. So I overrided to_param in model/post.rb and use friendly_id for title:
extend FriendlyId
friendly_id :title, :use => :slugged
def to_param
"#{year}/#{month}/#{day}/#{title.parameterize}"
end
And I added this to routes.rb:
resources :posts do
collection do
match ":year/:month/:day/:id", :action => "show"
end
member do
post 'comment'
delete 'comment/:comment_id', :action => 'destroy_comment', :as => 'destroy_comment'
end
end
In show this works:
<%= link_to image_tag("/images/edit_icon.png"),
{ :controller => 'posts', :action => 'edit' }, :id => #post %>
But in index it says
routing error:
No route matches {:action=>"edit", :controller=>"posts"}.
I'm new to Rails and I haven't managed to find out what causes this error. Can anyone help me figure out what I do wrong?

Rails 3 Correctly routing the destroy action for a session

I am refactoring my access_controller into a sessions_controller and can't seem to get my destroy action working properly.
Logging in seems to work fine, but I am unable to log out of a session. Here is the link I have for logging out:
<%= link_to("Logout", :controller => "sessions", :action => 'destroy') %>
routes.rb
resources :sessions
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
...
end
def destroy
session[:user_id] = nil
flash[:notice] = "You are now logged out"
redirect_to root_url
end
end
When I click "Logout" I get redirected to "/sessions/destroy" with a message of "The action 'show' could not be found for SessionsController". The destroy actions seems to want an id, but I don't need to pass in an id, I just want to run the action.
Ah, I found the answer here: http://railscasts.com/episodes/250-authentication-from-scratch
I need to set up my routes as follows:
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
resources :sessions

Rails 3: Problem with a route to new action

I have a problem with rails 3 routes.
I want add a new action called "gestion_etudiant" with a new view "gestion_etudiant.html.erb".
I have on my index page a link like this
<%= link_to "Administration", {:controller => "users", :action => "gestion_etudiant"} %>
I also try this:
<%= link_to "Administration", "/users/gestion_etudiant" %>
In my controller:
def gestion_etudiant
#users = User.find(:all)
end
but when I clic on the link, I always have this error:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with ID=gestion_etudiant
I have this in my routes file:
resources :users
And I've also try to add:
match "users/gestion_etudiant", :to => "users#gestion_etudiant"
and
resources :users, :only => [:gestion_etudiant]
But I can not access my page "gestion_etudiant.html.erb". Can anybody suggest why?
Try this:
# router:
resources :users do
get :gestion_etudiant, :on => :collection
end
# view:
link_to "Administration", gestion_etudiant_users_path
# Controller
def gestion_etudiant
#users = User.all # Don't use find with :all as it will be deprecated in rails 3.1
end
In your routes, try:
resources :users do
collection do
get 'gestion_etudiant'
end
end
You can check the routes you have in your application by running rake routes