2014-06-11 36 views
3

我想弄清楚如何使用簡單的窗體與Rails,Devise,Omniauth和Bootstrap。我有幾個問題,但其中之一是錯誤通知。簡單的窗體錯誤通知

我有一個設計登錄和註冊形式內Bootstrap模態。登記表如下。請注意,我從表格中刪除了required: true函數,因爲我不喜歡將星號附加到標籤上(但我確實嘗試將其返回以查看錯誤通知是否可用)。

<% if user_signed_in? %> 
    <li> 
    <%= link_to('Edit registration', edit_user_registration_path) %> 
    </li> 
<% else %> 
    <li> 
    <%= link_to 'Register', new_user_registration_path, :remote => true, 'data-toggle' => "modal", 'data-target' => "#myRModal", :method => 'get' %> 
    </li> 

    <div class="modal fade" id="myRModal" tabindex="-1" role="dialog" aria-labelledby="myRModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
      <h4 class="modal-title" id="myModalLabel" style="color:black">Register</h4> 
     </div> 

     <div class="modal-body"> 
      <%- if devise_mapping.omniauthable? %> 
      <div class="facebookauth"> <%= link_to "Register with Facebook", user_omniauth_authorize_path(:facebook) %></div> 
      <br /> 
      <% end -%> 
      <%- if devise_mapping.omniauthable? %> 
      <div class="linkedinauth"> <%= link_to "Register with LinkedIn", user_omniauth_authorize_path(:linkedin) %></div> 
      <br /> 
      <% end -%> 
     </div> 

     <div class="modal-footer"> 



    <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs" style="padding-left:20%; text-align:left; color:black;"> 

     <%= f.input :first_name, placeholder: 'Enter your first name', autofocus: true, :input_html => {:maxlength => 15, :size => 40} %> 
     <%= f.input :last_name, placeholder: 'Enter your surname', autofocus: true, :input_html => {:maxlength => 15, :size => 40} %> 
     <%= f.input :email, placeholder: 'Enter email', autofocus: true, :input_html => {:maxlength => 25, :size => 40} %> 
     <%= f.input :password, placeholder: 'At least 8 characters', :input_html => {:maxlength => 15, :size => 40} %> 
     <%= f.input :password_confirmation, placeholder: 'Confirm your password', :input_html => {:maxlength => 15, :size => 40} %> 
    </div> 

    <div class="form-actions" style="padding-left:20%; padding-top:5%; text-align:left; color:black;"> 
     <%= f.button :submit, "Register" %><% end %> 

我有一個用戶模式驗證電子郵件地址和密碼的存在。

我有以下命令來創建功能的用戶/ registration_controller:

def create 
    @user = User.new(user_params)#(params[:user]) 

    respond_to do |format| 
    if @user.save 
     # Tell the UserMailer to send a welcome email after save 
     # AdminMailer.new_user_waiting_for_approval.deliver 

     format.html { redirect_to(root_path, notice: 'Registration received.') } 
     format.json { render json: root_path, status: :created, location: @user } 
    else 
     format.html { redirect_to(root_path, alert: 'Sorry! There was a problem with your registration. Please contact us to sort it out.') } 
     # format.html { render action: 'new' } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
    end 
    end 
end 

我有設計在我的兩個應用助手的助手,並制定幫手如下:

def resource_name 
    :user 
end 

def resource 
    @resource ||= User.new 
end 

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

我的初始化文件有兩個簡單的表單文件。一個是simple_form.rb。另一種是simple_form_bootstrap.rb

沒有錯誤在窗體中內聯顯示。當我填寫沒有電子郵件地址的表單時,出現我的registration_controller的create函數中出現的錯誤。

當用戶點擊提交時,我真的很希望在表單中以內聯方式顯示錯誤。

有誰知道如何解決這個問題?

謝謝。

+0

是通過AJAX提交的表單嗎? – Sharagoz

回答

2

請確保您有在烏拉圭回合初始的包裝

b.use :error, wrap_with: { tag: 'span', class: 'help-block' }

如您在烏爾問題中提到類似的線,請刪除required: false, 並要求刪除「*」,改變它在配置/ locale/simple_form.en.yml

required: 
    text: 'required' 
    mark: '' 
+0

謝謝。我寫了自定義jquery來處理錯誤(所以沒有試圖編輯簡單的形式或簡單的形式引導初始化與上述建議。我真的不明白爲什麼標準函數不與這些寶石,但工作圍繞這個問題。謝謝關於*在yml文件中的提示。 – Mel

2

有誰知道如何解決這個問題?

因爲您可以使用registration表單中的對象,這意味着您對如何顯示錯誤有一些自由。這將是一個不同的故事,如果你試圖達到同樣的用login(如登錄不使用的一種形式)

-

我們以前(你這樣做可以看到here - 點擊「註冊」,在頂部):

#app/views/devise/registrations/new.html.erb 
<%= f.email_field :email, :placeholder => "email" %> 
<div class="error"><%= devise_resource.errors[:email].first if devise_resource.errors[:email].present? %></div> 

考慮形式在form_for對象使用devise_resource - 這應該讓你牛逼o顯示錯誤內聯

+0

嗨Rich,謝謝你向我展示你的例子。我認爲我原來的帖子出了問題。註冊表格的開頭沒有出現在我的問題的公開文本中。我正在使用簡單形式的寶石。它有一個f.error_notification函數。我想知道是否干擾了錯誤通知。我嘗試添加類似於你的代碼到我的表單中,但它不起作用。我想知道是否有簡單表單中的某些內容對此問題有所貢獻。感謝您分享您的方法 - 如果我無法解決這個問題,也許我可以卸載簡單表單並按照您的方式嘗試。 – Mel

1

你說你在使用模態?但是你不會發布阿賈克斯(你的表單上沒有remote: true)。所以我會描述我現在想要發生的事情。您不發佈Ajax,因此您發佈html,因此它選擇format.html路徑,錯誤時您的控制器重定向到根路徑,並顯示錯誤消息。相反,它應該重新呈現表單。

因此,取消註釋創建操作中的註釋行應該工作:)(並且顯然刪除當前的format.html行)。

Ofcourse重新渲染不會是一個模式。在使用模態時,如果您希望表單保持打開狀態,則必須使用ajax,並在發生錯誤時將表單替換爲重新呈現的版本。

簡單的形式,給出的驗證,錯誤的對象時,會自動顯示錯誤的內聯。

+0

感謝您關注此事。我已經更新了這個問題,以包含模態的開始行(顯示遠程真實)。我需要在表單中再次使用它嗎?我不知道如何用重新呈現的表單替換表單。你知道怎麼做嗎?即使在刪除並重新安裝設計和簡單形式之後 - 錯誤也不會在表單中顯示爲內聯。我真的很想了解如何讓他們展示。你是否知道爲什麼他們不能渲染,內聯,幾乎每個博客和文檔都與簡單的表單和設計相關聯?謝謝 – Mel

+0

讓我明白這一點:當你點擊「註冊」鏈接時,你會顯示模式,它包含兩個鏈接,用Facebook或LinkedIn註冊? Afaik的'data-toggle'會阻止跟隨鏈接的默認動作,所以你只能看到兩個鏈接。那是對的嗎? – nathanvda

+0

看來你在同一時間處理很多:設計和簡單形式是新的,Ajax形式是新的,可能甚至軌道是你的新手,然後你想要做omniauth。我會專注於準備好簡單的註冊流程,並逐漸使其變得更加困難,以便您可以逐步完成。嬰兒的步驟。 – nathanvda

1

,錯誤驗證的問題得到解決,如果:

  1. 刪除:從simple_form通話f.error_nofication;和
  2. 插入:<%= devise_error_messages! %>在表單輸入字段中。

如果您對用戶模型進行了驗證並且還使用了devise validatable,則表單頂部的錯誤列表將複製錯誤。刪除一個或另一個驗證,你將會有錯誤的工作。

我想不通爲什麼簡單的形式不渲染錯誤 - 但這是一個解決方案。

我決定從我的應用程序中刪除簡單的形式 - 我不能閱讀張貼在堆棧溢出有關使用它與色器件沒有得到回答的文檔和這麼多問題。我看不到它的好處。

至於模態 - 我會用這個來試試。 (手指交叉)。我希望這個答案有助於某人。我一直在設法設計3個月 - 用3周的時間努力瞭解文檔。