2012-12-12 47 views
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" 

,其工作正常,但問題來了與驗證,當我想不輸入任何數據來註冊用戶。 ..顯示錯誤消息,但它也着重登錄形式的用戶名和密碼字段..有沒有辦法避免這種驗證混淆?

回答

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| 
+0

謝謝!我解決了它,但錯誤只顯示當我將simple_form_for標記更改爲:simple_form_for(@login_resource || resource ... –