Rails Devise: How to access sign up page after signed in?

后端 未结 3 1353
我寻月下人不归
我寻月下人不归 2021-02-10 07:46

I am new with rails and i am using \"devise\" gem for authentication purposes.

At first i add a new user through default sign up page (E.g./users/sign_up)

Then,

相关标签:
3条回答
  • 2021-02-10 08:20

    The way I handled the scenario of being able to create New Users if you are signed in was by generating a User controller and having new and create action methods that would save to the User model. (This was stripped from a current app I'm working on so hopefully I didn't miss anything)

    user_controller.rb

    def new
      @user = User.new
    end
    
    def create
      @user = User.new(params[:user])
    
      if @user.save
        flash[:notice] = "Successfully created User." 
        redirect_to root_path
      else
        render :action => 'new'
      end
    end
    

    views/user/new.html.erb

    <%= form_for @user, :url => user_index_path do |f| %>
      <p><%= f.label :email %>
      <%= f.text_field :email %></p>
    
      <p><%= f.label :password  %>
      <%= f.password_field :password %></p>
    
      <p><%= f.label :password_confirmation  %>
      <%= f.password_field :password_confirmation %></p>
    
      <p><%= f.submit "Save" %></p>
    <% end %>
    

    config/routes.rb (Rails 3)

    resources :user, :controller => "user"
    

    Link to the New User page

    <%= link_to 'New User', new_user_path %>
    
    0 讨论(0)
  • 2021-02-10 08:31

    If you are getting redirected it probably means you are not properly authenticated when you navigate to that page, seeing as it requires a valid user session.

    Please post your Registrations controller file as well.

    If you are getting redirected it probably means you are not properly authenticated when you navigate to that page, seeing as it requires a valid user session.

    Please post your Registrations controller file as well.

    Addition:

    As you can see in the devise source if you navigate to the sign_up it executes the before_filter require_no_authentication and this redirects to the root path which you can find here.

    I think you will have to explicitly override the registrations_controller that I linked first if you really want to override this behaviour :-)

    0 讨论(0)
  • 2021-02-10 08:38

    I have other decision. Bitterzoet said

    As you can see in the devise source if you navigate to the sign_up it executes the before_filter require_no_authentication and this redirects to the root path which you can find here.

    You don't need override registration_controller, you can change only your custom registration_controller that echo original registration_controller.

    class Admin::RegistrationsController < Devise::RegistrationsController
      layout 'admin'
      prepend_before_filter :require_no_authentication, :only => []
      prepend_before_filter :authenticate_scope!
    end
    
    0 讨论(0)
提交回复
热议问题