2

我有这个表格部分:

.row
    .span6
        .well
            %h2 Sign in
            = simple_form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => {:class => 'form-vertical' }) do |f|
                = f.input :email, :autofocus => true, :wrapper => :bootstrap_placeholder
                = f.input :password, :wrapper => :bootstrap_placeholder
                = f.input :remember_me, :as => :boolean if devise_mapping.rememberable?
                = f.button :submit, "Sign in", :class => 'btn-primary'
            = render "devise/shared/links"



    .span6
        .well
            %h2 Sign up
            = simple_form_for(resource, :as => resource_name, :url => regi stration_path(resource_name), :html => {:class => 'form-vertical' }) do |f|
                = f.error_notification
                = display_base_errors resource
                = f.input :name, :autofocus => true
                = f.input :email, :required => true
                = f.input :password, :required => true
                = f.input :password_confirmation, :required => true
                = f.button :submit, 'Sign up', :class => 'btn-primary'
            = render "devise/shared/links"

并且它工作正常,但问题来自验证,当我想在不输入任何数据的情况下注册用户时......显示错误消息,但它也突出显示登录表单的用户名和密码字段..有没有办法避免这种验证混淆?

4

1 回答 1

1

resource两种形式都是一样的,所以如果两者都显示错误是正常的。为了防止这种情况,您必须分离您使用的资源。尝试这样的事情。

# registrations_controller.rb
def create
  super
  @registration_resource = resource
end

# sessions_controller.rb
def create
  super
  @login_resource = resource
end

# _form.html.slim
# sign up
simple_form_for(@registration_resource || resource_name.to_s.capitalize.constantize.new, :as => resource_name, :url => regi stration_path(resource_name), :html => {:class => 'form-vertical' }) do |f|

# sign in
simple_form_for(@login_resource || resource_name.to_s.capitalize.constantize.new, :as => resource_name, :url => regi stration_path(resource_name), :html => {:class => 'form-vertical' }) do |f|
于 2012-12-12T13:43:47.080 回答