ANeidley
ANeidley

Reputation: 25

How to redirect_to root after users/sign_up with devise?

Running Rails (v7.0.1), ruby (v3.0.1) and added the devise the gem to the Gemfile.

I'm testing the users/sign_up route in the browser on localhost:3000/users/sign_up. It successfully saves the user to the database (postgresql) I want to redirect_to root after sign_up, just like the sign_in currently does, but instead I get:

NoMethodError in Devise::RegistrationsController#create undefined method `user_url' for #Devise::RegistrationsController:0x00000000032988 Extracted source (around line #231): 229 230 231 232 233 234

        if options.empty?
          recipient.public_send(method, *args)
        else
          recipient.public_send(method, *args, options)
        end

Here's the stacktrace:

Started POST "/users" for ::1 at 2022-02-03 15:06:14 -0600
Processing by Devise::RegistrationsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  TRANSACTION (0.2ms)  BEGIN
  User Exists? (0.3ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
  User Create (0.5ms)  INSERT INTO "users" ("email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["email", "[email protected]"], ["encrypted_password", "[FILTERED]"], ["reset_password_token", "[FILTERED]"], ["reset_password_sent_at", "[FILTERED]"], ["remember_created_at", nil], ["created_at", "2022-02-03 21:06:14.495644"], ["updated_at", "2022-02-03 21:06:14.495644"]]
  TRANSACTION (0.8ms)  COMMIT
Redirected to
Completed 500 Internal Server Error in 313ms (ActiveRecord: 1.8ms | Allocations: 7490)



NoMethodError (undefined method `user_url' for #<Devise::RegistrationsController:0x00000000032988>):

I've tried modifying the Application controller using:

ApplicationController.rb

def after_sign_up_path_for(:user)
 root_path
end

(https://github.com/heartcombo/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in-out)

but no luck.

Also tried rails generate devise:controllers registrations to expose the necessary registrations#create method for tinkering, but haven't cracked it (https://stackoverflow.com/a/41972901/9495921)

Upvotes: 1

Views: 346

Answers (1)

mechnicov
mechnicov

Reputation: 15258

You can create your own registrations controller as devise registrations controller child with after_sign_up_path_for method

# app/controllers/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  private

  def after_sign_up_path_for(resource)
    root_path # or any other path
  end
end

And then add this controller to your routes to devise_for like this

# config/routes.rb

devise_for :users, controllers: { registrations: "registrations" }

Upvotes: 2

Related Questions