Rails 4 - 简单表单,Cocoon Gem嵌套字段 - 错误消息

时间:2016-04-03 02:25:13

标签: ruby-on-rails twitter-bootstrap simple-form cocoon-gem

我正在尝试使用Rails 4制作应用程序。

我正在为嵌套字段使用cocoon gem,以便嵌套子集。

我也在使用bootstrap。

我很挣扎,因为如果出现错误,我的页面会显示:“请查看以下问题:”但是没有任何内容突出显示,我找不到错误的位置。

如何设置嵌套字段以在父页面中显示错误 - 甚至在嵌套的部分表单中也可以。

我在我的主要形式中使用了许多部分,

当我尝试编辑配置文件(主)对象时,我可以在控制台中看到更新似乎已提交,但是表单再次使用上面的错误消息呈现(但没有任何标识错误是什么)

Started PATCH "/profiles/9" for ::1 at 2016-04-03 13:54:26 +1000
  ActiveRecord::SchemaMigration Load (0.9ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ProfilesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"39DIH7j7LeX7bnUnLirqKHpVPZ7vxmeKN+Q==", "profile"=>{"title"=>"", "occupation"=>"", "working_languages"=>"", "external_profile"=>"", "overview"=>"", "byline"=>"", "industry_ids"=>[""], "personality_attributes"=>{"average_day"=>"", "fantasy_project"=>"", "preferred_style"=>""}, "addresses_attributes"=>{"0"=>{"unit"=>"", "street_number"=>"1", "street"=>"Martin Street", "building"=>"", "city"=>"London", "region"=>"UK", "zip"=>"EC1A2PG", "country"=>"UK", "time_zone"=>"Eastern Time (US & Canada)", "main_address"=>"1", "project_offsite"=>"0", "_destroy"=>"false", "id"=>"1"}}}, "commit"=>"Submit", "id"=>"9"}
  User Load (1.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 9]]
  Profile Load (0.5ms)  SELECT  "profiles".* FROM "profiles" WHERE "profiles"."id" = $1 LIMIT 1  [["id", 9]]
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 9]]
   (0.3ms)  BEGIN
  Industry Load (0.8ms)  SELECT "industries".* FROM "industries" INNER JOIN "industries_profiles" ON "industries"."id" = "industries_profiles"."industry_id" WHERE "industries_profiles"."profile_id" = $1  [["profile_id", 9]]
  Personality Load (0.5ms)  SELECT  "personalities".* FROM "personalities" WHERE "personalities"."profile_id" = $1 LIMIT 1  [["profile_id", 9]]
  SQL (0.6ms)  UPDATE "personalities" SET "profile_id" = $1, "updated_at" = $2 WHERE "personalities"."id" = $3  [["profile_id", nil], ["updated_at", "2016-04-03 03:54:26.755593"], ["id", 10]]
  Address Load (0.8ms)  SELECT "addresses".* FROM "addresses" WHERE "addresses"."addressable_id" = $1 AND "addresses"."addressable_type" = $2 AND "addresses"."id" = 1  [["addressable_id", 9], ["addressable_type", "Profile"]]
  Profile Load (0.5ms)  SELECT  "profiles".* FROM "profiles" WHERE "profiles"."id" = $1 LIMIT 1  [["id", 9]]
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 9]]
   (0.3ms)  ROLLBACK
  Rendered users/_profileimgform.html.erb (6.2ms)
  Industry Load (0.6ms)  SELECT "industries".* FROM "industries"
  Qualification Load (0.5ms)  SELECT "qualifications".* FROM "qualifications" WHERE "qualifications"."profile_id" = $1  [["profile_id", 9]]
  Rendered qualifications/_qualification_fields.html.erb (34.5ms)
  Job Load (0.4ms)  SELECT "jobs".* FROM "jobs" WHERE "jobs"."profile_id" = $1  [["profile_id", 9]]
  Rendered jobs/_job_fields.html.erb (33.1ms)
  Rendered personalities/_form.html.erb (6.6ms)
  Vision Load (0.4ms)  SELECT  "visions".* FROM "visions" WHERE "visions"."profile_id" = $1 LIMIT 1  [["profile_id", 9]]
  Rendered visions/_form.html.erb (8.5ms)
  Address Load (0.3ms)  SELECT "addresses".* FROM "addresses" WHERE "addresses"."addressable_id" = $1 AND "addresses"."addressable_type" = $2  [["addressable_id", 9], ["addressable_type", "Profile"]]
  Rendered addresses/_address_fields.html.erb (262.2ms)
  Rendered addresses/_address_fields.html.erb (53.3ms)
  Rendered profiles/_form.html.erb (551.8ms)
  Rendered profiles/edit.html.erb within layouts/profile (561.3ms)
  Profile Load (0.4ms)  SELECT  "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT 1  [["user_id", 9]]
  Rendered pages/_nav.html.erb (7.8ms)
  Rendered pages/_footer.html.erb (1.5ms)
Completed 200 OK in 1908ms (Views: 1646.7ms | ActiveRecord: 29.7ms)

对于cocoon gem,地址表格如下:

<div class="nested-fields">

  <div class="container-fluid">

                <div class="form-inputs">
                    <div class="row">
                        <div class="col-xs-3">
                            <%= f.input :unit %>
                        </div>
                        <div class="col-xs-3 col-xs-offset-1">
                            <%= f.input :street_number %>
                        </div>
                        <div class="col-xs-3 col-xs-offset-1">
                            <%= f.input :street %>
                        </div>
            </div>

            <div class="row">
                        <div class="col-xs-3">
                            <%= f.input :building %>
                        </div>
                        <div class="col-xs-3 col-xs-offset-1">
                            <%= f.input :city %>
                        </div>
                        <div class="col-xs-3 col-xs-offset-1">
                            <%= f.input :region %>
                        </div>
            </div>

                   <div class="row">
                    <div class="col-xs-3">
                        <%= f.input :zip %>
                    </div>
                    <div class="col-xs-3 col-xs-offset-1">

                <!--f.country_select(:country, {selected: @profile.country_name}, {class: "form-control"}) -->
              <%=   f.country_select  :country, priority: [ "Australia", "New Zealand", "United Kingdom" ]  %>

                    </div>
            <div class="col-xs-3 col-xs-offset-1">
              <%= f.input :time_zone %>
            </div>


           </div>

           <div class="row">
                    <div class="col-xs-3">
                        <%= f.input :main_address %>
                    </div>
                    <div class="col-xs-3 col-xs-offset-1">
                        <%= f.input :project_offsite %>
                    </div>
           </div>

           <div class="row">
            <div class="col-md-6">
            </div>

            </div>
           </div>

         <div class="row">
          <div class="col-md-6">
            <%= link_to_remove_association 'Remove this address', f %>
          </div>

        </div>
  </div>    
</div>

<div class = "debug" > <%= yield %> <%= debug(params) if Rails.env.development? %></div>

然后在我的个人资料表格中添加:

       <%= link_to_add_association 'Add an address', f, :addresses, partial: 'addresses/address_fields' %>

这令人沮丧,因为我无法找到任何错误可能导致更新停止处理的指示。

2 个答案:

答案 0 :(得分:1)

看起来您的嵌套表单不确定它们与哪些相关联。您应该在输入字段中嵌套按钮。

<div class="row-fluid">
  <div class="col-md-12">
    <h3>Your Title</h3>
    <div id="Your-form">
      <%= f.simple_fields_for :your_tables do |table| %>
        <%= render 'table_fields', f: table %>
      <% end %>
      <div class="links"></div>
      <%= link_to_add_association 'Add', f, :your_tables,  class: 'btn btn-secondary add-button' %>
    </div>
  </div>
</div>

并且有一部分看起来像这样。

<div class="form-inline clearfix">
  <div class="row">
    <div class="nested-fields">
      <%= f.input :table,  placeholder: "Enter a Value", label: false, input_html: { class: "form-input form-control", style: 'width:80%;'} %>
      <%= link_to_remove_association "Remove", f, class: "form-control btn btn-secondary", style: 'height:20%; display:inline; float:right;'  %>
    </div>
  </div>
</div>

答案 1 :(得分:0)

如果没有任何示例,很难说你是如​​何使用这些嵌套表格的,但我们假设。

使用关联中的默认验证器时,应始终突出显示有错误的元素,如果您使用自定义验证器,请确保将错误添加到正确的关联中,否则表单中不会显示任何内容。