簡體   English   中英

注冊后設計重定向

[英]Devise redirect after sign up

設計讓我很難過。 目前,除了注冊重定向之外,其他一切似乎都在工作。 我想設計在索引操作時重定向到我的城鎮控制器,在注冊或登錄時(登錄實際上有效)。

我試過覆蓋 RegistrationsController 並嘗試添加一個 ApplicationsController 函數,如:

  def after_sign_in_path_for(resource_or_scope)
    if resource_or_scope.is_a?(User)
      town_path
    else
      super
    end
  end

盡管如此,我還是遇到了同樣的錯誤:

NoMethodError in User/townController#index

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.*

說真的,我找不到辦法做到這一點。 請問有什么想法嗎? :)

編輯:我的路線

      new_user_session GET    /users/sign_in(.:format)                       {:action=>"new", :controller=>"devise/sessions"}
          user_session POST   /users/sign_in(.:format)                       {:action=>"create", :controller=>"devise/sessions"}
  destroy_user_session GET    /users/sign_out(.:format)                      {:action=>"destroy", :controller=>"devise/sessions"}
         user_password POST   /users/password(.:format)                      {:action=>"create", :controller=>"devise/passwords"}
     new_user_password GET    /users/password/new(.:format)                  {:action=>"new", :controller=>"devise/passwords"}
    edit_user_password GET    /users/password/edit(.:format)                 {:action=>"edit", :controller=>"devise/passwords"}
                       PUT    /users/password(.:format)                      {:action=>"update", :controller=>"devise/passwords"}
     user_registration POST   /users(.:format)                               {:action=>"create", :controller=>"devise/registrations"}
 new_user_registration GET    /users/sign_up(.:format)                       {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET    /users/edit(.:format)                          {:action=>"edit", :controller=>"devise/registrations"}
                       PUT    /users(.:format)                               {:action=>"update", :controller=>"devise/registrations"}
                       DELETE /users(.:format)                               {:action=>"destroy", :controller=>"devise/registrations"}
                  root        /(.:format)                                    {:action=>"index", :controller=>"home"}
             user_root        /user(.:format)                                {:action=>"index", :controller=>"user/town"}
                  home        /home(.:format)                                {:action=>"index", :controller=>"home"}
                  town        /town(.:format)                                {:action=>"index", :controller=>"town"}
                 inbox        /messages(.:format)                            {:action=>"index", :controller=>"messages"}
                 inbox        /messages/inbox(.:format)                      {:action=>"inbox", :controller=>"messages"}

路線.rb :

  devise_for :users

  root :to => "home#index"

  namespace :user do
    root :to => "town#index"
  end  

  scope :path => '/home', :controller => :home do
    match '/' => :index, :as => 'home'
  end

  scope :path => '/town', :controller => :town do
    match '/' => :index, :as => 'town'
  end
......

我使用Devise 1.3.4與Ruby On Rails 3.0.7。 在檢查了互聯網的解決方案后,我所做的只是粘貼以下代碼

* 首先) *要在成功注冊到歡迎頁面后重定向,請在/config/routes.rb中放置以下內容(注意,替換:user其中包含您為devise_for提供的參數):

namespace :user do
root :to => "welcome#index"
end

* second) *要在注銷后重定向(也到歡迎頁面),請將此方法放在/app/controllers/application_controller.rb中:

private
# Overwriting the sign_out redirect path method
def after_sign_out_path_for(resource_or_scope)
welcome_index_path
end

這對我有用,我希望它適用於所有人。

這就是我的工作方式。

# In your routes.rb
match 'dashboard' => 'user_dashboard#index', :as => 'user_root'

然后確保你沒有before_filter :authenticate_user! 在你的home#index控制器上設置。

這個問題的答案對我來說非常有效,在Devise Wiki上有解釋:
如何:成功注冊時重定向到特定頁面(注冊)

由於沒有人回答,我會插話。你確定這是導致錯誤的代碼嗎? 你的resource_or_scope是nil使得看起來像Devise是問題,但我懷疑是這樣的。 所以我認為問題出在其他地方。

我會嘗試以下方法,以確保它首先工作。

  def after_sign_in_path_for(resource)
    "http://www.google.com"
  end

如果確實有效,那么嘗試檢查resource變量以確保它不是零。

托尼說的話。 命名空間不應該以這種方式使用。 我不知道是誰提出這個根本不起作用的黑客,但不幸的是,這是谷歌搜索時出現的第一個解決方案之一。

namespace :user do
  root :to => "town#index"
end

將'/ user'設置為user_root_path並指向您沒有的'user / town'控制器的索引操作。 你擁有的是“城鎮”控制器。 這就是你得到錯誤的原因。 你需要的是什么

get '/town', to: 'town#index', as: 'user_root'

這樣,'/ town'指向'town #index'並設置為你的user_root_path,這是用戶在用戶之后重定向到的

  1. 登入
  2. 注冊
  3. 更新密碼

所以你甚至不需要覆蓋after_sign_in_path_for ,這與許多人認為的相反,不是解決設計重定向問題的最佳位置。

在Rails 4.1+和Devise 3.2上

你有沒有嘗試覆蓋sign_in_and_redirect方法; 它會對你有用

def sign_in_and_redirect(resource_or_scope,resource)
    if resource_or_scope == :user
      redirect_to town_path
    else
      super
    end
end

不確定這是否有用但我遇到了上述相同的問題,但不同的是我正在使用自定義注冊控制器。

從RegistrationsController的設計源代碼看來,它正在調用sign_in_and_redirect,后者又將其傳遞給redirect_location方法。

我重寫redirect_location只是從after_sign_up_path_for(資源)而不是stored_location_for(范圍)||返回路徑。 after_sign_up_path_for(資源)。

在after_sign_up_path_for(資源)中,我根據用戶類型自定義重定向到正確路徑的方法

適用於Rails 3.0.5和Devise 1.3.0。

問題是,當簽入剛剛創建的資源時,Devise會繞過after_sign_in_path_for(resource) 你需要的是after_sign_up_path_for(resource)

# override in your ApplicationController or custom Devise::RegistrationsController  
def after_sign_up_path_for(resource)
  if resource.is_a?(User)
    town_path
  else
    super
  end
end

您仍然需要使用after_sign_in_path_for(resource)覆蓋來正確地將后續登錄重定向到資源。

我遇到了類似的問題。 最終在應用程序控制器中使用if語句。 如果用戶已登錄且他們沒有配置文件,則路由到new_profile_path

只是為了添加其他重要的輸入; 如果您在模型中使用可confirmable屬性,您可能會忽略的一件事是,如果您想更改after_sign_up_path ,則非活動帳戶使用不同的方法,如設計維基上所述: https : //github.com/heartcombo /devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-%28registration%29

在這種情況下,請使用:

class RegistrationsController < Devise::RegistrationsController
  protected
    def after_inactive_sign_up_path_for(resource)
      '/an/example/path'
    end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM