rails 5.1:嵌套表单验证

时间:2017-11-06 21:57:34

标签: ruby ruby-on-rails-5

我有一个名为user的父模型和两个名为award and certification的嵌套模型。当我去保存认证时,奖励的模型验证告诉我奖项是空白的,当我得到保存奖励时,我得到一个错误,表明认证的验证是空白的。不确定发生了什么。

这是我的用户模型

  has_many :certifications, dependent: :destroy
  has_many :awards, dependent: :destroy


  # allows for nested attributes in the user views.
  accepts_nested_attributes_for :work_histories, :qualifications, :certifications, :courses, :awards,
  :languages, :patents, :publications, allow_destroy: true 

这是我的奖励模式

    class Award < ApplicationRecord
  belongs_to :user

  validates :award_name, :award_granted_by, :award_date, :award_description, presence: true  

end

和我的认证模式

class Certification < ApplicationRecord
  belongs_to :user

  validates :certification_name, :certification_authority, :certification_number, :certification_from,
            :certification_to, presence: true

end

users_controller中的强大参数

 def user_params
   params.require(:user).permit( :first_name, :last_name, :email, :phone, :current_job_title, :password, :password_confirmation, 
   work_histories_attributes: [:user_id, :id, :_destroy, :job_title, :company, :description, :city, :state,:start_date, :end_date],
   qualifications_attributes: [:user_id, :id, :_destroy, :education, :university, :university_major, :graduation_year, :currently_enrolled, :location],
   awards_attributes: [:user_id, :id, :_destroy, :award_name, :award_granted_by, :award_date, :award_description],                            
   certifications_attributes: [:user_id, :id, :_destroy, :certification_name, :certification_authority, :certification_number, :certification_from, :certification_to, :cert_never_expires],                           
   courses_attributes: [:user_id, :id, :_destroy, :course_name, :course_number, :course_description],
   languages_attributes: [:user_id, :id, :_destroy, :language, :language_proficiency],
   patents_attributes: [:user_id, :id, :_destroy, :patent_title, :patent_office, :patent_number, :patent_status, :patent_date, :patent_description],
   publications_attributes: [:user_id, :id, :_destroy, :publication_title, :publication_source, :publication_date, :publication_description] )
 end   

这是User表单的一部分。我有一个按钮下拉列表,有几个选项,当用户选择一个选项时,特定的div将显示一个模态窗口。

  <!-- one-to-many nested attributes -->  
  <%= form_for(@user) do |form| %>

      <!-- a partial is rendered based on the user dropdown selection -->    
      <div id="award">
        <%= render partial: "awards/new_award_modal", locals: { form: form } %>
      </div>  
      <div id="cert">
        <%= render partial: "certifications/new_certification_modal", locals: { form: form } %>
      </div>                    

    <%= form.submit 'Save', id: "submit-achievement", class: 'btn btn-primary form-control' %>

  </div><!-- modal body -->

    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>

  <% end %><!-- form_for -->  

这是认证的部分内容

<div class="new_cert"> 

<%= form.fields_for :certifications, Certification.new do |cert_field| %>

  <div class="field">
    <%= cert_field.label :certification_name, 'Certification Title' %>
    <%= cert_field.text_field :certification_name, class: "form-control" %>       
  </div>
  <div class="field">
    <%= cert_field.label :certification_authority %>
    <%= cert_field.text_field :certification_authority, class: "form-control" %>       
  </div>
  <div class="field">
    <%= cert_field.label :certification_number %>
    <%= cert_field.text_field :certification_number, class: "form-control" %>       
  </div>

  <table>
    <tr>
      <td>
      <div class="field">
        <%= cert_field.label :certification_from, "From" %><br />
        <%= cert_field.date_select :certification_from,{ :order => [:day, :month, :year],
            :start_year => Date.current.year, :end_year => 1960,
            :prompt => {day: 'Day', month: 'Month', year: 'Year'}, :discard_day => true }, class: 'form-control' %>
      </div>     
      </td>
      <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
      <td>
      <div class="field">
        <%= cert_field.label :certification_to, "To" %><br />
        <%= cert_field.date_select :certification_to, { :order => [:day, :month, :year], 
            :start_year => Date.current.year + (10), :end_year => 1960,
            :prompt => {day: 'Day', month: 'Month', year: 'Year'}, :discard_day => true }, class: 'form-control'  %>
      </div>        
      </td>
    </tr>
  </table>  

  <div>
    <%= cert_field.label :cert_never_expires, "This certification does not expire", class: "l" %>
    <%= cert_field.check_box :cert_never_expires, class: 'cb' %>
  </div>
  <br />

<% end %>

</div><!-- ./new_cert -->

这是奖项部分

<div class="new_award">

  <!-- nested attributes for award modal -->  
  <%= form.fields_for :awards, Award.new do |award_field| %>

      <div class="field">
        <%= award_field.label :award_name, 'Award or Honor Name' %>
        <%= award_field.text_field :award_name, class: "form-control" %>       
      </div>

      <div class="field">
        <%= award_field.label :award_granted_by, "Granted by" %>
        <%= award_field.text_field :award_granted_by, class: "form-control" %>       
      </div>

      <div class="field form-inline">
        <%= award_field.label :award_date, 'Date' %><br />
        <%= award_field.date_select :award_date, { :order => [:day, :month, :year],
            :start_year => Date.current.year, :end_year => 1960,
            :prompt => {day: 'Day', month: 'Month', year: 'Year'}, 
            :discard_day => true }, class: 'form-control ds' %>
      </div>     

      <div class="field">
        <%= award_field.label :award_description, 'Description' %>
        <%= award_field.text_area :award_description, class: "form-control" %>       
      </div>

  <% end %><!-- ./fields_for -->

</div>

因此,如果没有验证,奖励和认证都可以保存到数据库而不会出现任何问题。然而,当我只是做一个简单的验证检查表单中是否存在属性时,事情变得奇怪了。奖励模式是说认证表格是空的,应该是因为我正在提交奖励表格。反之亦然,当我提交认证时,会触发奖励验证,表示奖励表格为空白。

1 个答案:

答案 0 :(得分:1)

最好能够看到您遇到的具体错误,但我确保您的表单正确设置嵌套属性,并确保您的user控制器具有嵌套属性设置在强大的参数范围内。

Rails指南中的文档:http://api.rubyonrails.org/classes/ActionController/StrongParameters.html