Ruby on rails 在Ruby和Rails中使用accepts\u嵌套的\u属性\u存储时出错

Ruby on rails 在Ruby和Rails中使用accepts\u嵌套的\u属性\u存储时出错,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一份问卷表,每一份记录(一些信息)都有很多服务 模型 class Record < ActiveRecord::Base has_many :services, :dependent => :destroy accepts_nested_attributes_for :services, :reject_if => lambda {|a| a[:name].blank? }, :allow_destroy => true end 观点是 &l

我有一份问卷表,每一份记录(一些信息)都有很多服务

模型

class Record < ActiveRecord::Base
    has_many :services, :dependent => :destroy
    accepts_nested_attributes_for :services, :reject_if => lambda {|a| a[:name].blank? }, :allow_destroy => true
end
观点是

    <%= form_for(@record) do |f| %>
      <% if @record.errors.any? %>
         <div id="error_explanation">
          <h2><%= pluralize(@record.errors.count, "error") %> prohibited this record from being saved:</h2>

            <ul>
             <% @record.errors.full_messages.each do |message| %>
               <li><%= message %></li>
             <% end %>
            </ul>
         </div>
       <% end %>

       <div class="field">
         <%= f.label :name %>
         <br>
         <%= f.text_field :name %>
       </div>
       <div class="field">
         <%= f.label :department %>
         <br>
         <%= f.text_field :department %>
       </div>
       <div class="field">
         <%= f.label :description %>
         <br>
         <%= f.text_area :description %>
       </div>
       <div class="field">
         <%= f.label :is_automated %>
         <br>
         <%= f.check_box :is_automated %>
       </div>
       <div class="field">
         <%= f.label :automation_ratio %>
         <br>
         <%= f.number_field :automation_ratio %>
       </div>
       <div class="field">
         <%= f.label :db_name %>
         <br>
         <%= f.text_field :db_name %>
       </div>
       <div class="field">
         <%= f.label :is_licensed %>
         <br>
         <%= f.text_field :is_licensed %>
       </div>
       <div class="field">
         <%= f.label :notes %>
         <br>
         <%= f.text_area :notes %>
       </div>
       <div class="field">
         <%= f.label :emp_name %>
         <br>
         <%= f.text_field :emp_name %>
       </div>
       <div class="field">
         <%= f.label :emp_mobile %>
         <br>
         <%= f.text_field :emp_mobile %>
       </div>

       <table>
         <tr>
           <th>
            Name
            </th>
            <th>
            Description
            </th>
            <th>
            Range
            </th>
            <th>
            Customer
            </th>
          </tr>

         <%= f.fields_for :services do |builder| %>
            <tr>
              <td>
              <%= builder.text_field :name %>
              </td>
              <td>
              <%= builder.text_area :description %>
              </td>
              <td>
              <%= builder.text_field :range %>
              </td>
              <td>
              <%= builder.text_field :customer %>
              </td>
            </tr>
         <% end %>

        </table>

       <div class="actions">
         <%= f.submit %>
       </div>
   <% end %>

正如@G.B.在评论中指出的,您已将两款机型命名为
Record
(我不确定这是否是一个打字错误)

class记录:销毁
接受_嵌套的_属性_for:services,:reject _if=>lambda{a | a[:name].blank?},:allow _destroy=>true
结束

class Record课堂记录
谢谢,现在一切都好了,我已经陷入这个问题大约一周了:)
def new
    @record = Record.new
    5.times { @record.services.build }
end
....

def record_params
  params.require(:record).permit(
      :name, :department, :description, :is_automated, :automation_ratio,
      :db_name, :is_licensed, :notes, :emp_name, :emp_mobile,
      services_attributes: [
      :id, :name, :range, :description, :customer
      ]
  )
end
    <%= form_for(@record) do |f| %>
      <% if @record.errors.any? %>
         <div id="error_explanation">
          <h2><%= pluralize(@record.errors.count, "error") %> prohibited this record from being saved:</h2>

            <ul>
             <% @record.errors.full_messages.each do |message| %>
               <li><%= message %></li>
             <% end %>
            </ul>
         </div>
       <% end %>

       <div class="field">
         <%= f.label :name %>
         <br>
         <%= f.text_field :name %>
       </div>
       <div class="field">
         <%= f.label :department %>
         <br>
         <%= f.text_field :department %>
       </div>
       <div class="field">
         <%= f.label :description %>
         <br>
         <%= f.text_area :description %>
       </div>
       <div class="field">
         <%= f.label :is_automated %>
         <br>
         <%= f.check_box :is_automated %>
       </div>
       <div class="field">
         <%= f.label :automation_ratio %>
         <br>
         <%= f.number_field :automation_ratio %>
       </div>
       <div class="field">
         <%= f.label :db_name %>
         <br>
         <%= f.text_field :db_name %>
       </div>
       <div class="field">
         <%= f.label :is_licensed %>
         <br>
         <%= f.text_field :is_licensed %>
       </div>
       <div class="field">
         <%= f.label :notes %>
         <br>
         <%= f.text_area :notes %>
       </div>
       <div class="field">
         <%= f.label :emp_name %>
         <br>
         <%= f.text_field :emp_name %>
       </div>
       <div class="field">
         <%= f.label :emp_mobile %>
         <br>
         <%= f.text_field :emp_mobile %>
       </div>

       <table>
         <tr>
           <th>
            Name
            </th>
            <th>
            Description
            </th>
            <th>
            Range
            </th>
            <th>
            Customer
            </th>
          </tr>

         <%= f.fields_for :services do |builder| %>
            <tr>
              <td>
              <%= builder.text_field :name %>
              </td>
              <td>
              <%= builder.text_area :description %>
              </td>
              <td>
              <%= builder.text_field :range %>
              </td>
              <td>
              <%= builder.text_field :customer %>
              </td>
            </tr>
         <% end %>

        </table>

       <div class="actions">
         <%= f.submit %>
       </div>
   <% end %>
Started POST "/records" for 127.0.0.1 at 2014-06-18 14:11:12 +0300
Processing by RecordsController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"pOtpKNAzczg1NyMmYG3xe5Hf4rb/WxUO7Kbo/FxaG9I=", "record"=>{"name"=>"MOT system", "department"=>"HR", "description"=>"", "is_automated"=>"1", "automation_ratio"=>"100"
, "db_name"=>"oracle", "is_licensed"=>"true", "notes"=>"", "emp_name"=>"Abdallah Hussien", "emp_mobile"=>"000000000", "services_attributes"=>{"0"=>{"name"=>"Vacation System", "description"=>"Online Vacation System",
"range"=>"--------", "customer"=>"All Employees"}, "1"=>{"name"=>"User Management", "description"=>"Manage User Accounts", "range"=>"--------", "customer"=>"HR Team"}, "2"=>{"name"=>"", "description"=>"", "range"=>""
, "customer"=>""}}}, "commit"=>"Create Record"}
←[1m←[36m (0.0ms)←[0m  ←[1mbegin transaction←[0m
←[1m←[35mSQL (1.0ms)←[0m  INSERT INTO "records" ("automation_ratio", "created_at", "db_name", "department", "description", "emp_mobile", "emp_name", "is_automated", "is_licensed", "name", "notes", "updated_at") 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["automation_ratio", 100], ["created_at", "2014-06-18 11:11:12.414355"], ["db_name", "oracle"], ["department", "HR"], ["description", ""], ["emp_mobile", "000000000"], ["emp
_name", "Abdallah Hussien"], ["is_automated", "t"], ["is_licensed", "true"], ["name", "MOT system"], ["notes", ""], ["updated_at", "2014-06-18 11:11:12.414355"]]
←[1m←[36mSQL (0.0ms)←[0m  ←[1mINSERT INTO "records" ("created_at", "updated_at") VALUES (?, ?)←[0m  [["created_at", "2014-06-18 11:11:12.418355"], ["updated_at", "2014-06-18 11:11:12.418355"]]
←[1m←[35mSQL (0.0ms)←[0m  INSERT INTO "records" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2014-06-18 11:11:12.420356"], ["updated_at", "2014-06-18 11:11:12.420356"]]
←[1m←[36mSQL (0.0ms)←[0m  ←[1mINSERT INTO "records" ("created_at", "updated_at") VALUES (?, ?)←[0m  [["created_at", "2014-06-18 11:11:12.422356"], ["updated_at", "2014-06-18 11:11:12.422356"]]
←[1m←[35m (36.0ms)←[0m  commit transaction
Redirected to http://localhost:3000/records/17
Completed 302 Found in 55ms (ActiveRecord: 37.0ms)
class Record < ActiveRecord::Base
    has_many :services, :dependent => :destroy
    accepts_nested_attributes_for :services, :reject_if => lambda {|a| a[:name].blank? }, :allow_destroy => true
end


class Record < ActiveRecord::Base  ##<-- This should be class Service
    belongs_to :record
end