用户注册后设计不重定向到指定页面

时间:2016-05-11 02:02:51

标签: ruby-on-rails-4 devise

问题:我使用的是Devise提供的after_sign_up_path_for(resource)方法,但在注册后,我似乎无法将用户重定向到指定的页面。目前,我收到Template Missing错误,其网址为www.localhost:3000/users。理想情况下,我希望将用户重定向到www.localhost:3000/subscribers/new

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  def create
    @user = User.new(sign_up_params)
    @domain = @user.email.split('@').last
    @company = Company.find_by_domain(@domain)
    @user.company_id = @company.id

    if @user.save
      after_sign_up_path_for(@user)
    else
      render 'new'
    end
  end

  private

    def sign_up_params
      params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :participation_code, :company_id)
    end


  protected

    def after_sign_up_path_for(resource)
      "/subscribers/new"
    end
end

的routes.rb

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

错误记录我

Template is Missing

Missing template registrations/create, devise/registrations/create,    
devise/create, application/create with {:locale=>[:en], :formats=  
[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby,   
:coffee, :arb, :haml, :jbuilder]}.

更新:我已尝试将after_sign_up_path_for(resource)移至ApplicationController但仍获得相同的结果。

4 个答案:

答案 0 :(得分:0)

非常有趣。我只是改变了

def create
...
end

为...

def update
...
end

现在一切正常,我的after_sign_up_path_for(resource)方法得到了认可。

答案 1 :(得分:-1)

在应用程序控制器中添加以下方法

设计:一旦用户登录后重定向的位置

 def after_sign_up_path_for(resource)
   "http://www.google.com" # <- Path you want to redirect the user to.
 end

答案 2 :(得分:-1)

也许值得将redirect_method放在after_action中:创建回调?

答案 3 :(得分:-1)

您不需要创建控制器,您可以通过定义

将用户重定向到特定路径
def after_sign_in_path_for(resource)
   your_path
 end

记得使用after_sign_in_path_for而不是after_sign_up_path_for