サインアップ後のリダイレクトを考案する 質問する

サインアップ後のリダイレクトを考案する 質問する

Devise は、かなり苦労しています。今のところ、サインアップ リダイレクト以外はすべて機能しているようです。サインアップまたはログイン時に、インデックス アクションで、Devise がタウン コントローラーにリダイレクトするようにしたいです (ログインは実際に機能します)。

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
......

ベストアンサー1

私はRuby On Rails 3.0.7でDevise 1.3.4を使用しています。インターネットで解決策を調べた後、次のコードを貼り付けるだけでした。

*初め)*サインアップが成功した後にウェルカム ページにリダイレクトするには、/config/routes.rb に次の内容を配置します (:userに指定した引数に置き換えてくださいdevise_for)。

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

*2番目)*サインアウト後にリダイレクトするには(ウェルカム ページにもリダイレクト)、/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

これは私にとってはうまくいきました。皆さんにもうまくいくことを願っています。

おすすめ記事