如何在一个视图中正确组合登录和注册表单

时间:2012-12-12 13:23:21

标签: ruby-on-rails devise

我有这种形式的部分:

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

并且它工作正常,但问题是验证,当我想注册用户而不输入任何数据...显示错误消息但它也高亮登录表单的用户名和密码字段..是否有一种避免这种验证混淆的方法吗?

1 个答案:

答案 0 :(得分: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|