Rails Devise redirect after signup - ruby-on-rails

I have rails app that uses Devise with confirmable by email. Emails are send thru gmail. When user fill registration form then is redirected to "/user", but this path doesn't exists hence app crashes. Is there anything that I can do, to force redirect after submit to index?
Yes I've already tried Devise Wiki
thanks

Before you ask such questions please take some time and search for the solution by yourself.
It took me 30 seconds to find the Solution on a Wiki page of Devise.
Just create a RegistrationsController which inherits from Devise::RegistrationsController and add a #after_sign_up_path_for method to it.
Then just add devise_for :users, controllers: { registrations: "registrations" } to your routes if you have users as resource.

Related

Devise showing unauthenticated message instead of signed_up_but_unconfirmed

I am using the default Devise controllers. I assumed when a user created a new account, they'd be redirected to the sign_in page and shown the flash message for signed_up_but_unconfirmed. The redirect is happening, but the flash message being displayed is unauthenticated.
Why would I need to tell Devise to show the signed_up_but_unconfirmed message when new accounts are created? That seems like the way it should work out of the box.
And more importantly, how do I configure it to show signed_up_but_unconfirmed?
I finally found an answer in an older question. The short of it is this:
Override the registration controller:
rails g devise:controllers users -c=registrations
Uncomment the after_inactive_sign_up_path_for method in the registratons_controller:
def after_inactive_sign_up_path_for(resource)
new_user_session_path
end

Determine redirect paths after sign up with Devise

I can have two types of users sign up on my app, "girls" and "boys". If a girl signs up I want to redirect to "/girls" and if a boy signs up I want to redirect to "/boys".
Is it possible to do custom redirection with Devise?
The closest docs I found are here: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in. The problem is I can't do any check to switch the redirect route.
Options I've considered:
Pass an additional URL param when user clicks "sign-up". like
?is_girl=1.
After they click sign-up, when determining the redirect route, I could look at the users model and see if they're a girl or boy. Then redirect accordingly.
I am going to assume as part of the sign up process you ask them if they are a boy or girl and this is saved in the database.
So you would just need to do like the example in the Devise docs is showing
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
if resource.sex == 'boy'
'/boy' # or you could create a route in routes.rb and do boy_path
else
'/girl' # with routes setup: girl_path
end
end

Multiple, Simultaneous Oauth in a Rails Application?

My end goal is for users to have multiple 3rd party authentications at the same time.
Right now, I am using Devise to create users. Users can sign up via email or facebook or google and it works. But now, after they have already signed up, I need them to also verify with, say, youtube or soundcloud. So the user was created with devise, but I also need them to verify with other things.
Since Devise hogs omniauth for it's own purposes, I can't use omniauth on the side.
As I see it I have three options:
Try to monkeypatch devise and get it to allow multiple authentications at the same time on one user
Do oauth by hand on the side adjacent to current Devise implementation
Scrap Devise and do something different
I would greatly appreciate any advice or other options
I think this may be what you need: http://blog.joshsoftware.com/2010/12/16/multiple-applications-with-devise-omniauth-and-single-sign-on/
They open sourced their code too!
Provider: https://github.com/joshsoftware/sso-devise-omniauth-provider
Client: https://github.com/joshsoftware/sso-devise-omniauth-client
Or even better, check out this: http://communityguides.heroku.com/articles/16
Try to monkeypatch devise and get it to allow multiple authentications at the same time on one use
You don't need to monkeypatch devise --- you can have your own oauth controller the has
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# handle if already a twitter user
# handle if a new user
# use the `sign_in user` to sign_in the user
end
def twitter
# handle if already a facebook user
# handle if a new user
end
end
and use it in routes
devise_for :user,
:controllers => {
:omniauth_callbacks => "users/omniauth_callbacks"
}

rails Devise - How to redirect to a Getting Started Link - After validating an email

I'm using Devise with my rails 3 app. The app requires users to validate their email before continuing.
How can I redirect users to a specific url like /gettingstarted after they successfully validate their email address via the email confirmation msg they receive?
Thanks
When a user clicks on the confirm link they are taken to a confirm page which checks the confirmation token and if it's valid automatically logs them into the application. You could overwrite the after_sign_in_path_for method in your ApplicationController (as shown on the Devise wiki) and then redirect them to your getting started page the first time a user logs in.
def after_sign_in_path_for(resource_or_scope)
if resource_or_scope.is_a?(User) && first login
getting_started_path
else
super
end
end
For "first login" you could test if the confirmed_at timestamp is within a couple minutes of now, if your also using the trackable module in devise you can check if the sign_in_count is 1 or you could create your own field in the user model that tracks this information.
I'm checking devise source code at https://github.com/plataformatec/devise/blob/master/app/controllers/devise/confirmations_controller.rb
and seems that we have a callback to do it "after_confirmation_path_for"but I couldn't get it working without rewrite Devise::ConfirmationController
I hope that helps and if somebody get it working just defining after_confirmation_path_for just let us know.
I'm using the last_sign_in_at field from the 'trackable' model to achieve this. I've got the following code in my root action:
if current_user.last_sign_in_at.nil? then
redirect_to :controller => :users, :action => :welcome
end
http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Trackable
Seems to work reasonably well.
inside the 'after_sign_in_path_for' the current_user.last_sign_in_at.nil? will not work since it is alerady after the first sign-in. However this will work
if current_user.sign_in_count == 1
# do 1 thing
else
# do another thing
end

I want to customise devise gem's controllers, is it possible and how to do?

Is there a way to customise the devise controllers , as we can modify the devise views using the "rails g devise:views" generator command. ??
Ok purpose here is to create a statistics table's row for the current user as soon as a user is registered.
I have a user statistics maintained for every user.I just want to trigger the create method of the userstats controller in the background when a user sign-up for my web app.
Is there a way to do this ?
You need to create your own controllers inheriting from Devise's.
class Admins::SessionsController < Devise::SessionsController
end
Then you tell devise to use that controller:
devise_for :admins, :controllers => { :sessions => "admins/sessions" }
And copy your views from devise/sessions, to admin/sessions.
You can read it here: https://github.com/plataformatec/devise
Or simply do this:
rails generate devise:controllers Admin
or copy the devise controllers from where they are now to your app. This is what I did with RVM:
cp -R ~/.rvm/gems/ruby-1.9.3-p194#my_gemset/gems/devise-2.1.0/app/controllers/* my_rails_app/app/controllers/

Resources