2017-06-24 22 views
0

deviseを使用しているルートに問題があります。リダイレクトルーティングの問題を解決する

私のdevise/sessions/newビューを変更して、サインインフォームとサインアップフォームの両方を持つようにしました。

すべて正常に動作し、すべてのフィールドが正しく入力されていれば、同じビュー(sign_in)のいずれかのフォームを使用してサインインまたはサインアップできます。しかし、登録フォームにエラーがある場合(電子メールがないか、確認パスワードが間違っている場合)、ユーザーはusers/sign_inページにある代わりにhttp://0.0.0.0:3000/usersというURLの「古典的な」デバイスsign_upページにリダイレクトされ、エラーが表示されます。

私のルートは以下の通りである:

devise_for :users, controllers: { sessions: "users/sessions"} 
as :user do 
    get 'sign_in', :to => 'devise/sessions#new' 
end 

マイ登録フォームは、次の宣言している:

<%= form_for(resource, as: resource_name, class: "registrtion-form", url: registration_path(resource_name)) do |f| %> 

マイアプリケーションヘルパーはユーザ・リソースを設定:

def resource_name 
    :user 
    end 

    def resource 
    @resource ||= User.new 
    end 

    def devise_mapping 
    @devise_mapping ||= Devise.mappings[:user] 
    end 

答えて

1

私はあなたがそれを行うには、RegistrationsController.createメソッドをオーバーライドできると思います。

class User::RegistrationsController < Devise::RegistrationsController 
    def create 
    build_resource(sign_up_params) 

    resource.save 
    yield resource if block_given? 
    if resource.persisted? 
     if resource.active_for_authentication? 
     set_flash_message! :notice, :signed_up 
     sign_up(resource_name, resource) 
     respond_with resource, location: after_sign_up_path_for(resource) 
     else 
     set_flash_message! :notice, :signed_up_but_#{resource.inactive_message}" 
     expire_data_after_sign_in! 
     respond_with resource, location: after_inactive_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords resource 
     set_minimum_password_length 
     # respond_with resource 
     # custom code to redirect to your sign up form if have an error in the registration form. 
     redirect_to ... 
    end 
    end 
end 

希望します。