设计错误重定向到不正确的表单

时间:2014-09-05 13:50:16

标签: ruby-on-rails ruby-on-rails-4 devise

我刚刚注意到在抛出验证错误时我的设计注册表单出现问题

我有两种形式,一种用于个人,一种用于公司,以区分两种

<p><%= link_to 'Sign Up as Individual', new_user_registration_path %></p>
<p><%= link_to 'Sign Up as Vets/Rescue Centre', new_user_registration_path(organization: true) %></p>

查看

<% if params[:organization] %>
  <%= render 'shared/company_signup_form' %>
<% else %>
  <%= render 'shared/individual_signup_form' %>
<% end %>

如果在company_signup_form上验证失败,它会将我重定向到individualal_signup_form(这是新用户注册的默认网址)

有没有办法告诉表单重定向到正在使用的表单?

修改

我已在注册控制器中覆盖了一个方法

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
   administration_index_path
  end
end

这会导致问题吗?

由于

1 个答案:

答案 0 :(得分:1)

对于自定义重定向,您必须覆盖registrations_controller上的创建操作

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    build_resource(sign_up_params)
    resource_saved = resource.save
    yield resource if block_given?
    if resource_saved
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_flashing_format?
        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}" if is_flashing_format?
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      @validatable = devise_mapping.validatable?
      if @validatable
        @minimum_password_length = resource_class.password_length.min
      end
      if resource.is_a? Organization
        redirect_to new_user_registration_path(organization: true)
      else
        redirect_to new_user_registration_path
      end
    end
  end
end

修改

您需要配置设计路线:

devise_for :users, controllers: { registrations: 'users/registrations' }