Adding customer action for devise - ruby-on-rails

I'm trying to add a simple custom action cancelaccount to a user model generated by devise, but having a hard time getting the routing right.
Here is what's in route.rb
devise_for :users, controllers: { registrations: "userregistrations", sessions: "sessions" } do
get '/users/:id/cancelaccount', to: 'userregistrations#cancelaccount', as: 'cancelaccount'
end
Here is the controller
class UserregistrationsController < Devise::RegistrationsController
def create
...
end
def cancelaccount
authenticate_user!
Rails.logger.debug {"&& cancel"}
#user = User.find(params[:id])
unless (#user == current_user)
redirect_to :back, :alert => "Access denied."
end
end
end
Here is the link_to line in the HTML:
<li><%= link_to "Cancel Account", {controller: "userregistrations", action: "cancelaccount", id: current_user.id} %></li>
This is the error I get:
No route matches {:action=>"cancelaccount", :controller=>"userregistrations", :id=>12}
If I change the link_to link to:
<li><%= link_to "Cancel Account", cancelaccount_path(#user) %></li>
The error is:
undefined method `cancelaccount_path' for #<#:0x00000006a5d4c0>
Any help is highly appreciated.
Here is the result of "rake routes":
new_user_session GET /users/sign_in(.:format) sessions#new
user_session POST /users/sign_in(.:format) sessions#create
destroy_user_session DELETE /users/sign_out(.:format) sessions#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) devise/omniauth_callbacks#passthru {:provider=>/(?!)/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) devise/omniauth_callbacks#:action
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
cancel_user_registration GET /users/cancel(.:format) userregistrations#cancel
user_registration POST /users(.:format) userregistrations#create
new_user_registration GET /users/sign_up(.:format) userregistrations#new
edit_user_registration GET /users/edit(.:format) userregistrations#edit
PATCH /users(.:format) userregistrations#update
PUT /users(.:format) userregistrations#update
DELETE /users(.:format) userregistrations#destroy
root GET / static_pages#home
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
userpreferences GET /userpreferences(.:format) userpreferences#index
POST /userpreferences(.:format) userpreferences#create
new_userpreference GET /userpreferences/new(.:format) userpreferences#new
edit_userpreference GET /userpreferences/:id/edit(.:format) userpreferences#edit
userpreference GET /userpreferences/:id(.:format) userpreferences#show
PATCH /userpreferences/:id(.:format) userpreferences#update
PUT /userpreferences/:id(.:format) userpreferences#update
DELETE /userpreferences/:id(.:format) userpreferences#destroy
signup GET /signup(.:format) devise/registrations#new
edit_profile GET /edit_profile(.:format) devise/registrations#edit
change_password GET /change_password(.:format) devise/passwords#edit
sign_in GET /sign_in(.:format) devise/sessions#new
sign_out GET /sign_out(.:format) devise/sessions#destroy
confirmation GET /confirmation(.:format) devise/confirmations#new
unlock_account GET /unlock_account(.:format) devise/unlocks#new
GET /signup(.:format) trainer/registrations#new
edit GET /edit(.:format) trainer/registrations#edit
GET /sign_in(.:format) devise/sessions#new
GET /sign_out(.:format) devise/sessions#destroy
GET /confirmation(.:format) devise/confirmations#new
GET /unlock_account(.:format) devise/unlocks#new

I am not sure what is wrong with your devise_for block, but you can easily add this route outside of it:
devise_for :users, controllers: { registrations: "userregistrations", sessions: "sessions" }
match '/users/:id/cancelaccount' => 'registrations#cancelaccount', via: 'get', as: 'cancelaccount'
Which will add the route you want:
cancelaccount GET /users/:id/cancelaccount(.:format) registrations#cancelaccount

Related

How can I access the profiles/show.html.erb in rails app

I´m using Devise, and after signing up, I want my users to access the profiles/show.html.erb
I have an registrations_controller.rb and in there I have this piece of code
def after_sign_up_path_for(resource)
new_user_profile_path(current_user)
end
It directs the user to profiles/new.html.erb but I want the user to go to profiles/show.html.erb
the rake routesshows this paths:
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
how would I modify this chunk of code to direct to the profiles/show.html.erb?
I'm to unexperienced to figure this out by my self, any help would be greate
this is my routes.rbfile
Rails.application.routes.draw do
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users do
resource :profile
end
root 'pages#index'
end
** Edit**
the rake routes output
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
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user_profile POST /users/:user_id/profile(.:format) profiles#create
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
PATCH /users/:user_id/profile(.:format) profiles#update
PUT /users/:user_id/profile(.:format) profiles#update
DELETE /users/:user_id/profile(.:format) profiles#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
GET /%20/users/:user_id/profile(.:format)%20 profiles#show
root GET / pages#index
I fixed it, check your github.
Spelling of your resource is wrong
It should be resources :profile not resource :profile
that is why it was not showing you the paths to the profile resources through rake routes command.
I used the profile_path(current_user) in the show action at profile_controller.rb file
In your controller you need to create
def show
end
You can also try replacing your code in registration controller with.
def after_sign_up_path_for(resource)
show_user_profile_path(current_user)
end
remove 'do' from resources :users do in routes.rb file.
def after_sign_up_path_for(resource)
user_path(current_user)
end
rake routes shows user GET /users/:id(.:format) users#show
routes.rb
get 'profiles/show', to: 'profiles/show', as: :profile
profiles_controller.rb
def show
unless params[:user_id] #user = User.find(current_user.id)
#user = User.find(params[:user_id])
end
and then
def after_sign_up_path_for(resource)
profile_path
end

Proper way of rails routing custom Users controller and devise gem

Im having a problem routing my custom Users controller and devise gem.
When i try to reach http://localhost:3000/users
<%= link_to 'Users', users_path, class: 'navbar-brand' %>
I get error:
Could not find devise mapping for path "/users". This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end
2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: #request.env["devise.mapping"] = Devise.mappings[:user]
If i run command rake routes i get this output:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
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
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
account_update PATCH /account_update(.:format) users#update
And this are my routes
root 'static_pages#home'
devise_for :users
resources :users
as :user do
patch 'account_update' => 'users#update'
get 'users' => 'users#index'
end
How to tell rails that on /users it should go to controller Users and select index page
You can try something like that,
devise_for :users
devise_scope :user do
end

how to set route with token as query string?

I am using devise and trying to use token code with devise registration page, I know I have some issue with the routes to set the link. The link looks like this
http://localhost:3000/users/sign_up.asdfsdfasdffffffffffffffasdfasdf
a dot in between the query string and it doesn't work.
my routes.rb is this
devise_for :users ,:controllers => { :registrations => "registrations" } do
#get "/register", :to => "devise/registrations#new"
get 'users/sign_up/:invitation_token' => "registrations#new", :as => :reg_with_code
end
please help me to set a proper link with the slash in between .
here is may rake routes
invitations GET /invitations(.:format) invitations#index
POST /invitations(.:format) invitations#create
new_invitation GET /invitations/new(.:format) invitations#new
edit_invitation GET /invitations/:id/edit(.:format) invitations#edit
invitation GET /invitations/:id(.:format) invitations#show
PUT /invitations/:id(.:format) invitations#update
DELETE /invitations/:id(.:format)
invitations#destroy
reg_with_code GET /users/sign_up/:invitation_token(.:format) registrations#new
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
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root / home#index
In the email template, change the token link to
<%= link_to "Sign Up", reg_with_code_path(#token) %>
since you have put a :as => :reg_with_code in you routes.rb file.
You can try with this:
reg_with_code_path(invitation_token: #token)

Devise: RoutingError prevents sign up of new users

I type in a new email, password, and password confirmation in my Devise app. When I go to actually do the signup, I get the following error:
RoutingError
No route matches [POST] "/users/sign_up"
Try running rake routes for more information on available routes.
It looks like new_registration_path takes you to users/sign_up, which it doesn't know about (at least when getting a POST). How do I make it recognize this?
Below are some relevant bits of code.
Here's the (possibly) relevant routes.rb:
devise_for :users
resources :users
Here's the output of rake routes:
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
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
static_pages_home GET /static_pages/home(.:format) static_pages#home
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
businesses GET /businesses(.:format) businesses#index
POST /businesses(.:format) businesses#create
new_business GET /businesses/new(.:format) businesses#new
ed it_business GET /businesses/:id/edit(.:format) businesses#edit
business GET /businesses/:id(.:format) businesses#show
PUT /businesses/:id(.:format) businesses#update
DELETE /businesses/:id(.:format) businesses#destroy
root / static_pages#home
Here's the form in registrations/new.html.erb:
<%= form_for(resource, :as => resource_name, :url => new_registration_path(resource_name)) do |f| %>
...
<% end %>
I think you should use registration_path instead of new_registration_path because sign up is performed via POST request to /users.
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
...
<% end %>

The action 'facebook' could not be found - devise/omniauth

I'm trying to implement facebook authentication in my app following this guide
After authorizing on Facebook I get the following:
Unknown action
The action 'facebook' could not be found for Devise::OmniauthCallbacksController
I've got the method implemented in app/controller/users/omniauth_callbacks_controller.rb as:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
My route for the callback is:
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
Is there something I'm missing? How can I go about debugging this?
Thanks!
Update: Added output of rake routes (sorry for the length):
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_omniauth_authorize /users/auth/:provider(.:format) devise/omniauth_callbacks#passthru {:provider=>/facebook/}
user_omniauth_callback /users/auth/:action/callback(.:format) devise/omniauth_callbacks#(?-mix:facebook)
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
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
users_new GET /users/new(.:format) users#new
signup /signup(.:format) users#new
new_certificate /new_certificate(.:format) users#newcertificate
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_omniauth_authorize /users/auth/:provider(.:format) users/omniauth_callbacks#passthru {:provider=>/facebook/}
user_omniauth_callback /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:facebook)
POST /users/password(.:format) devise/passwords#create
GET /users/password/new(.:format) devise/passwords#new
GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
GET /users/cancel(.:format) devise/registrations#cancel
POST /users(.:format) devise/registrations#create
GET /users/sign_up(.:format) devise/registrations#new
GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
GET /users/auth/:provider(.:format) users/omniauth_callbacks#passthru
courses GET /courses(.:format) courses#index
POST /courses(.:format) courses#create
new_course GET /courses/new(.:format) courses#new
edit_course GET /courses/:id/edit(.:format) courses#edit
course GET /courses/:id(.:format) courses#show
PUT /courses/:id(.:format) courses#update
DELETE /courses/:id(.:format) courses#destroy
enrollments GET /enrollments(.:format) enrollments#index
POST /enrollments(.:format) enrollments#create
new_enrollment GET /enrollments/new(.:format) enrollments#new
edit_enrollment GET /enrollments/:id/edit(.:format) enrollments#edit
enrollment GET /enrollments/:id(.:format) enrollments#show
PUT /enrollments/:id(.:format) enrollments#update
DELETE /enrollments/:id(.:format) enrollments#destroy
submissions GET /submissions(.:format) submissions#index
POST /submissions(.:format) submissions#create
new_submission GET /submissions/new(.:format) submissions#new
edit_submission GET /submissions/:id/edit(.:format) submissions#edit
submission GET /submissions/:id(.:format) submissions#show
PUT /submissions/:id(.:format) submissions#update
DELETE /submissions/:id(.:format) submissions#destroy
honour_code_signatures GET /honour_code_signatures(.:format) honour_code_signatures#index
POST /honour_code_signatures(.:format) honour_code_signatures#create
new_honour_code_signature GET /honour_code_signatures/new(.:format) honour_code_signatures#new
edit_honour_code_signature GET /honour_code_signatures/:id/edit(.:format) honour_code_signatures#edit
honour_code_signature GET /honour_code_signatures/:id(.:format) honour_code_signatures#show
PUT /honour_code_signatures/:id(.:format) honour_code_signatures#update
DELETE /honour_code_signatures/:id(.:format) honour_code_signatures#destroy
root / static_pages#home
Worked out what I had set up incorrectly! I initially followed a tutorial for Devise and added this to my routes:
devise_for :users
Then I followed the omniauth tutorial and added the following for the callbacks:
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
Somehow these must have been conflicting as simply removing the first record points the callback in the correct direction (it must have been looking to the Devise class that I sub-classed Devise::OmniauthCallbacksController).

Resources