Rails Devise: How to access sign up page after sig

2019-03-21 23:13发布

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, i made "sign_up" page only available to signed_in users by following instructions from

Devise before filter that prevents access to "new_user_registration_path" unless user is signed-in

Now, after sign in process when i try open sign up page it always directs me to root_path! How can i access sign up page?
My "roots.rb" file as follows:

Example::Application.routes.draw do

  devise_for :users, :controllers => { :registrations => 'registrations'}

  resources :companies

  resources :orders

  resources :customers

  root :to => "welcome#index"

end

Thank you all!

3条回答
等我变得足够好
2楼-- · 2019-03-21 23:25

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
查看更多
我只想做你的唯一
3楼-- · 2019-03-21 23:27

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 :-)

查看更多
forever°为你锁心
4楼-- · 2019-03-21 23:47

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 %>
查看更多
登录 后发表回答