Why does devise parameter sanitizer permit sign_up instead of user_registration - ruby-on-rails

class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
# Permit the `subscribe_newsletter` parameter along with the other
# sign up parameters.
devise_parameter_sanitizer.permit(:sign_up, keys: [:subscribe_newsletter])
end
end
Why is the first parameter here sign_up?
when typing `rails routes`,
I see
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
I'd assume user_registration (for the POST)instead:
devise_parameter_sanitizer.permit(:user_registration, keys: [:subscribe_newsletter])
I think there should be some logic in devise mapping sign_up to use_registration.
I can locate the permit function:
def permit(action, keys: nil, except: nil, &block)
if block_given?
#permitted[action] = block
end
if keys.present?
#permitted[action] ||= #auth_keys.dup
#permitted[action].concat(keys)
end
if except.present?
#permitted[action] ||= #auth_keys.dup
#permitted[action] = #permitted[action] - except
end
end
How to continue tracing from here?

It's just what they are called, no mapping here:
DEFAULT_PERMITTED_ATTRIBUTES = {
sign_in: [:password, :remember_me],
sign_up: [:password, :password_confirmation],
account_update: [:password, :password_confirmation, :current_password]
}
https://github.com/heartcombo/devise/blob/v4.8.1/lib/devise/parameter_sanitizer.rb#L38
Notice that permit method doesn't actually permit anything, it's just building a hash, and it's letting you add your own parameters.
Actual parameter permission is happening in sanitize method, where attributes held in sign_up are used to permit params submitted by the form, which are scoped to user. Something like this:
# params = {"user"=>{"email"=>"email", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}
# device_mapping = :user
# permitted = DEFAULT_PERMITTED_ATTRIBUTES
params[device_mapping].permit(permitted[:sign_up])

Related

display user's profile fail

Please read at the very bottom, I edited my post, I still need help.
regards
I am using devise to authenticate the users and admin with (admin: true). As an admin I want to visit the users profile's pages but I always arrive on my own profile ( as the current_user). I don't know how to do...
Users could see others users profile too
Thanks for your help
users/index.html.slim
.container
h1 All the users
.row
table.board
thead
tr
th First Name
th Last Name
th Email Address
th Action on User
hr
tbody.board
-#users.each do |user|
.row
.col-xs-3
= user.first_name
.col-xs-3
= user.last_name
.col-xs-3
= user.email
.col-xs-1
#The problem is this link
= link_to 'View', user_path(user.id), class:'btn btn-success'
.col-xs-1
= link_to 'Remove', user_path(user), class:'btn btn-danger', method: :delete, data: {confirm: "Are you sure?"}
hr
users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!
def show
#binding.pry
##user = User.find(current_user)
##user.id = User.find(params[:id])
#user = User.find(user_params[:id]) || current_user
#tutos= Tuto.all
end
def index
if current_user.admin == true
#users = User.all
else
redirect_to root_path
end
end
def destroy
#user = User.find(params[:id])
#user.destroy
flash[:success] = "User was successfully deleted"
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :id)
end
end
The routes
#edited
Rails.application.routes.draw do
namespace :users do
resources :tutos
end
resources :tutos, only: [:show]
resources :tutos do
member do
put "like", to: "tutos#upvote"
end
end
get "/register", to: "devise/registrations#new", as: :register
get "/login", to: "devise/sessions#new", as: :login
get "/logout", to: "devise/sessions#destroy", as: :logout
get "/account", to: "users#show", as: :account
get "/login" , to: "devise/sessions#new", as: :new_user_session
post "/login" , to: "devise/sessions#create", as: :user_session
delete "/logout" , to: "devise/sessions#destroy", as: :destroy_user_session
devise_for :users, skip: [:sessions]
resources :users
root "home#landing"
end
edit rake routes gives :
$ rake routes
Prefix Verb URI Pattern Controller#Action
users_tutos GET /users/tutos(.:format) users/tutos#index
POST /users/tutos(.:format) users/tutos#create
new_users_tuto GET /users/tutos/new(.:format) users/tutos#new
edit_users_tuto GET /users/tutos/:id/edit(.:format) users/tutos#edit
users_tuto GET /users/tutos/:id(.:format) users/tutos#show
PATCH /users/tutos/:id(.:format) users/tutos#update
PUT /users/tutos/:id(.:format) users/tutos#update
DELETE /users/tutos/:id(.:format) users/tutos#destroy
like_tuto PUT /tutos/:id/like(.:format) tutos#upvote
tutos GET /tutos(.:format) tutos#index
POST /tutos(.:format) tutos#create
new_tuto GET /tutos/new(.:format) tutos#new
edit_tuto GET /tutos/:id/edit(.:format) tutos#edit
tuto GET /tutos/:id(.:format) tutos#show
PATCH /tutos/:id(.:format) tutos#update
PUT /tutos/:id(.:format) tutos#update
DELETE /tutos/:id(.:format) tutos#destroy
register GET /register(.:format) devise/registrations#new
login GET /login(.:format) devise/sessions#new
logout GET /logout(.: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
account GET /account(.:format) users#show
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#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 /tutos(.:format) tutos#index
POST /tutos(.:format) tutos#create
GET /tutos/new(.:format) tutos#new
GET /tutos/:id/edit(.:format) tutos#edit
GET /tutos/:id(.:format) tutos#show
PATCH /tutos/:id(.:format) tutos#update
PUT /tutos/:id(.:format) tutos#update
DELETE /tutos/:id(.:format) tutos#destroy
root GET / home#landing
edit
after the last edits I still have a problem....
when I try to go on the account_path I have this error
Last edit
Just to remind you, I am using devise:
As a user logged in, if I want to see my own profile, I use
account_path (and this work well)
The link for visiting a user's profile page looks like this:
= link_to 'View', user_path(user)
but it looks like it point exactly like: account_path. ( So on my profile, not on the user I want to visit)
I am not sure what to use in my controller, if I use #user = User.find(user_params[:id]) || current_user or ##user = User.find(user_params[:id])
I have the following error:
ActionController::ParameterMissing in UsersController#show
param is missing or the value is empty: user
If I use #user = User.find(current_user).
I am redirected on my own profile each time....
def show
#binding.pry
##user = User.find(current_user)
##user = User.find(user_params[:id])
#user = User.find(user_params[:id]) || current_user
#tutos = Tuto.all
end
This will set #user to current user or to requested user for admins
#user = current_user.admin? ? User.find(params[:id]) : current_user
In #show you could something like:
# Assuming params[:id] is the ID of the user's profile you're trying to view
def show
user_id = current_user.admin? ? params[:id] : current_user.id
#user = User.find(user_id)
end
The problem is in your route file remove 'as user do' blocks it makes some bizzare things :
user GET /users/:id(.:format) users#show
GET /tutos/new(.:format) tutos#new
GET /tutos/:id/edit(.:format) tutos#edit
GET /tutos/:id(.:format) tutos#show
Do you want to use the account_path to view another user's profile, or could you go to /users/2 for the user with id of 2? That uses the show action in the controller and routes to the user path using the relevant id?

Rails 4: Edit/Update Threads, Edit/Update Posts

Before I begin yes I know I need a edit and update function in the posts and threads controller, but the issue I have is with the forum_post.user details getting lost in the update and the thread duplicating posts after the update, so I removed the code entirely so I can get help solving the problem by posting the controllers themselves.
But you're going to need the routes, before I post it /forum/ is just a fake route to nest the forum_threads/posts in and does not exist outside of it's scope.
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) 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
forum_thread_forum_posts GET /forum/forum_threads/:forum_thread_id/forum_posts(.:format) forum_threads/forum_posts#index
POST /forum/forum_threads/:forum_thread_id/forum_posts(.:format) forum_threads/forum_posts#create
new_forum_thread_forum_post GET /forum/forum_threads/:forum_thread_id/forum_posts/new(.:format) forum_threads/forum_posts#new
edit_forum_thread_forum_post GET /forum/forum_threads/:forum_thread_id/forum_posts/:id/edit(.:format) forum_threads/forum_posts#edit
forum_thread_forum_post GET /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#show
PATCH /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#update
PUT /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#update
DELETE /forum/forum_threads/:forum_thread_id/forum_posts/:id(.:format) forum_threads/forum_posts#destroy
forum_threads GET /forum/forum_threads(.:format) forum_threads#index
POST /forum/forum_threads(.:format) forum_threads#create
new_forum_thread GET /forum/forum_threads/new(.:format) forum_threads#new
edit_forum_thread GET /forum/forum_threads/:id/edit(.:format) forum_threads#edit
forum_thread GET /forum/forum_threads/:id(.:format) forum_threads#show
PATCH /forum/forum_threads/:id(.:format) forum_threads#update
PUT /forum/forum_threads/:id(.:format) forum_threads#update
DELETE /forum/forum_threads/:id(.:format) forum_threads#destroy
import_users POST /users/import(.:format) users#import
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
root GET / forum_threads#index
Routes:
Rails.application.routes.draw do
devise_for :users
scope "/forum" do
resources :forum_threads do
resources :forum_posts, module: :forum_threads
end
end
resources :users do
collection do
post :import
end
end
root 'forum_threads#index'
end
Forum Threads Controller
class ForumThreadsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_forum_thread, except: [:index, :new, :create]
def index
#q = ForumThread.search(params[:q])
#forum_threads = #q.result(distinct: true)
end
def show
#forum_post = ForumPost.new
end
def new
#forum_thread = ForumThread.new
#forum_thread.forum_posts.new
end
def create
#forum_thread = current_user.forum_threads.new forum_thread_params
#forum_thread.forum_posts.first.user_id = current_user.id
if #forum_thread.save
redirect_to #forum_thread
else
render action: :new
end
end
def destroy
#forum_thread.destroy
redirect_to root_path
end
private
def set_forum_thread
#forum_thread = ForumThread.find(params[:id])
end
def forum_thread_params
params.require(:forum_thread).permit(:subject, forum_posts_attributes: [:body])
end
end
Forum Posts Controller
class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread
def create
#forum_post = #forum_thread.forum_posts.new forum_post_params
#forum_post.user = current_user
if #forum_post.save
redirect_to forum_thread_path(#forum_thread, anchor: "forum_post_#{#forum_post.id}"), notice: "Successfully posted!"
else
redirect_to #forum_thread, alert: "Unable to save your post"
end
end
private
def set_forum_thread
#forum_thread = ForumThread.find(params[:forum_thread_id])
end
def forum_post_params
params.require(:forum_post).permit(:body)
end
end
I know the forum edit path for link_to will be edit_forum_thread_path or just correct me if I'm wrong, but it's the posts edit/delete path I need help with since that controller is nested under forum_threads and using the module forum_threads, I originally figured it would be edit_forum_threads_forum_posts_path but that wasn't it either last time I tried before I removed those functions.
It would be edit_forum_thread_forum_post_path based on your rake routes output.

How to have a 'model' index and then also a 'model' index for each user(models_path & user_models_path)

I have a Tool model and I want to be able to have both a regular tools index (tools_path) that lists all the tools in the database for all users & also an users tool index(users_tools_path) that lists all the tools for a particular user.
I am not sure what the rails way of implementing this is. I am using devise and my instinct was to do the following in my routes:
Rails.application.routes.draw do
devise_for :users
root 'tools#index'
resources :users do
resources :tools
end
resources :tools
end
This gets me the following 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
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
root GET / tools#index
user_tools GET /users/:user_id/tools(.:format) tools#index
POST /users/:user_id/tools(.:format) tools#create
new_user_tool GET /users/:user_id/tools/new(.:format) tools#new
edit_user_tool GET /users/:user_id/tools/:id/edit(.:format) tools#edit
user_tool GET /users/:user_id/tools/:id(.:format) tools#show
PATCH /users/:user_id/tools/:id(.:format) tools#update
PUT /users/:user_id/tools/:id(.:format) tools#update
DELETE /users/:user_id/tools/:id(.:format) tools#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
tools GET /tools(.:format) tools#index
POST /tools(.:format) tools#create
new_tool GET /tools/new(.:format) tools#new
edit_tool GET /tools/:id/edit(.:format) tools#edit
tool GET /tools/:id(.:format) tools#show
PATCH /tools/:id(.:format) tools#update
PUT /tools/:id(.:format) tools#update
DELETE /tools/:id(.:format) tools#destroy
This is my tools controller:
class ToolsController < ApplicationController
before_action :set_tool, only:[:show, :edit, :update, :destroy]
before_action :authenticate_user!, only:[:new, :destroy, :edit], notice: 'you must be logged in to proceed'
def index
#tools = Tool.all
end
def show
end
def new
#user = current_user
#tool = #user.tools.build
end
def create
#tool = Tool.new(tool_params)
#tool.save
redirect_to #tool
end
def edit
end
def update
#tool.update(tool_params)
redirect_to #tool
end
def destroy
#tool.destroy
redirect_to tools_path
end
private
def set_tool
#tool = Tool.find(params[:id])
end
def tool_params
params.require(:tool).permit(:name, :description)
end
end
These are my models:
class Tool < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :tools
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Unfortunately at this point when I navigate to users/id/tools all the tools in the database are listed instead of only the tools for that particular user. Also when I look at the active record instances of each tool in my database the user_id column is nil
However I am stuck here. Obviously the code will be different for each index so I can't use the same index action for both situations or the same index.html.erb view page either.
I am wondering wheat the next step would be?
Here are a few things I am thinking I could do:
1) create a new action in my tools controller something like user_index and include the user & tool logic to fetch the tools associated to a particular user. I would also have to create a new user_index.html.erb view with the view code. Then I would delete the nested resource and add a route like: match 'users/:id/tools' => 'tools#user_index', :via => get
2) I can delete the stand alone resources :tools route and the add a get :user_index, :on => :collection in my nested tool resource. Then add a user_index action to my controller like in solution #1. The only problem here is that then all my routes would have the user/:id prefix which I do not want.
What is the best solution in this case? Also, why are my active record tool instances not saving the id of the user that created them? Is there a way to get them to save the user id without nesting the resources?
What is the rails way for this?
To only show the tools of a particular user, instead of doing:
#tools = Tool.all
You can make an instance variable that is an array of Tools that are assigned to a particular user. For example:
#user = User.find(params[:id])
#tools = Tool.where(user_id: #user.id)
This will only collect the tools where the tools user_id matches the user ID of the user loaded into the show view.
Then when you do your loop, it will show the right tools for that user.
With the answer above you can use:
#tools = (#user.present?) ? #user.tools : Tool.all
To be able to set the user of each tool you have to edit your:``
params.require(:tool).permit(:name, :description)
To include user
params.require(:tool).permit(:name, :description, :user)

DEVISE after_inactive_sign_up_path_for not being called

Environment: RAILS 3.2 + DEVISE for auth + Invitable + Confirmable add-ons.
Using devise (2.2.3)
Using devise-i18n (0.6.5)
Using devise_invitable (1.0.3)
I am trying to redirect to a specific location after ACCEPT (TO SIGN UP), but only after_sign_in_path_for seems to be called after SIGN IN and ACCEPT.
I haven't been able to have after_accept_path_for working.
It continues to redirect to the "after sign in" location.
HERE THE CODE
In my routes.rb:
devise_for :users,
:controllers => { :registrations => 'registrations', :invitations => 'invitations' }
rake routes give me this:
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
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
accept_user_invitation GET /users/invitation/accept(.:format) devise/invitations#edit
user_invitation POST /users/invitation(.:format) devise/invitations#create
new_user_invitation GET /users/invitation/new(.:format) devise/invitations#new
PUT /users/invitation(.:format) devise/invitations#update
In my registration controller:
class RegistrationsController < Devise::RegistrationsController
# clear session
def create
super
session[:omniauth] = nil unless #user.new_record?
end
#protected
# after_sign_up_path_for doesn't seem to be called when using Confirmable module
# def after_inactive_sign_up_path_for(resource)
# #me_path
# session[:user_return_to].nil? ? me_path : session[:user_return_to]
# end
private
def build_resource(*args)
super
if session[:omniauth]
#user.apply_omniauth(session[:omniauth])
#user.valid?
end
end
end
Also
class Users::InvitationsController < Devise::InvitationsController
protected
def after_accept_path_for
session[:user_return_to].nil? ? me_path : session[:user_return_to]
end
end
In my application controller, (I left intentionally some commented code I tried to make it work):
def store_location
session[:user_return_to] = request.fullpath
end
# def after_sign_up_path_for
# me_path
# end
#
# def after_inactive_sign_up_path_for(resource)
# me_path
# #session[:user_return_to].nil? ? me_path : session[:user_return_to]
# end
# https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)
def after_sign_in_path_for(resource)
me_path
#dashboard_path
#session[:user_return_to].nil? ? dashboard_path : session[:user_return_to]
end
Any suggestions?
ADDED DEBUGGIN REDIRECTS
Add this to my application_controller
def redirect_to_with_logging(*args)
logger.debug "Redirect: #{args.inspect} from #{caller[0]}"
redirect_to_without_logging *args
end
alias_method_chain :redirect_to, :logging
After Sign in, works like a charm
Started POST "/users/sign_in" for 127.0.0.1 at 2013-04-25 14:20:04 +0200
Processing by Devise::SessionsController#create as HTML
[... I removed some of the Session creation info ...]
Redirect: ["/dashboard"] from /Users/joel/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.11/lib/action_controller/metal/responder.rb:135:in `redirect_to'
Redirected to http://localhost:3000/dashboard
Completed 302 Found in 968ms (ActiveRecord: 0.0ms)
DOCS:
After sign in
After sign up
After Accept <====
Override after_accept_path_for in Invitations controllers
class Users::InvitationsController < Devise::InvitationsController
protected
def after_accept_path_for(resource)
me_path
end
end
IMPORTANT:
put this file in 'controllers/users' directory
Fix the routes.rb to use the Users::InvitationsController
devise_for :users, :controllers => { :registrations => 'registrations', :invitations => 'users/invitations' }
You may put redirect_to my_specific_url at the end of sign_up controller's method, to redirect after signing up

Devise after_sign_in_path_for ... sending to .... root_path - query

I need help with a routes issue with devise authentication gem to redirect to a custom page after successful login so as to create a new record by entering a test person name and age ( test data )
I am using Rails 3 with devise version 1.4.9
My routes are as below
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
testers GET /testers(.:format) {:action=>"index", :controller=>"testers"}
POST /testers(.:format) {:action=>"create", :controller=>"testers"}
new_tester GET /testers/new(.:format) {:action=>"new", :controller=>"testers"}
edit_tester GET /testers/:id/edit(.:format) {:action=>"edit", :controller=>"testers"}
tester GET /testers/:id(.:format) {:action=>"show", :controller=>"testers"}
PUT /testers/:id(.:format) {:action=>"update", :controller=>"testers"}
DELETE /testers/:id(.:format) {:action=>"destroy", :controller=>"testers"}
root / {:controller=>"testers", :action=>"index"}
In applications controller i tried to override the method like below but to no avail i still get routed back to tester index
class ApplicationController < ActionController::Base
protect_from_forgery
def after_sign_in_path_for(resource)
new_tester_path
end
end
In my routes.rb file i have the below lines
Testing::Application.routes.draw do
devise_for :users
resources :testers
root :to => 'testers#index'
While much of the code was done with scaffolding I was still not be able to figure how to redirect to new_tester_path or route /testers/new after successful sign_in by user email and password.
Can someone please let me know what i am missing..... while writing the override function, I would like to know the exact route i need to specify.
While testing i tried something stupid like this but the google page is also not opening ... :(
class ApplicationController < ActionController::Base
protect_from_forgery
helper ApplicationHelper
def after_sign_in_path_for(resource)
"www.google.com"
end
def after_sign_up_path_for(resource)
"www.google.com"
end
def after_update_path_for(resource)
"www.google.com"
end
Just use this snippet:
class ApplicationController < ActionController::Base
def after_sign_in_path_for(user)
user_url(user)
end
end
Try setting user_return_to path in session:
session['user_return_to'] = new_tester_path
You can do it in a controller derived from Devise::SessionsController
The Devise documentation explains all the steps to redirect to a specific page on successful sign in. By combine the techniques, you can redirect a user to many places after successful sign in.
Here is the resume:
You can do this in a controller you inherit from Devise::SessionsController - first, in controllers/users/sessions_controller.rb:
module Users
class SessionsController < Devise::SessionsController
def new
if params[:redirect_to].present?
self.resource = resource_class.new(sign_in_params)
store_location_for(resource, params[:redirect_to])
end
super
end
end
end
In config/routes.rb, you would have also added:
devise_for :users, controllers: {sessions: 'users/sessions'}
And you must add a custom after_sign_in_path_for in your ApplicationController
class ApplicationController < ActionController::Base
protected
def after_sign_in_path_for(resource)
stored_location_for(resource) || root_path
end
end
This works in all Devise versions, as I know.
I believe this is an inheritance issue. after_sign_in_path_for is originally defined within Devise::SessionsController. You can override it by making your SessionsController inherit from Devise::SessionsController, and then re-defining it within that controller.
If you are having issues trying to override the after_sign_in_path_for or after_sign_out_path_for helper methods within the ApplicationController of a Rails engine, you may want to check out this answer.
It describes how you'll need to override the SessionsController in your engine instead of the ApplicationController.
You don't seem to be doing anything wrong. Maybe this is a Devise issue.
Can you please try to isolate this on a Rails app and open an issue on Devise ?

Resources