我有一份问卷表,每条记录(一些信息)都有很多服务
模型
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
belongs_to :record
end
记录控制器
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 %>
当我保存记录和(n)服务时,它不是在数据库中保存一条记录和(n)服务,而是保存记录和(n)空记录而不是服务
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)
答案 0 :(得分:0)
作为@ G.B。在评论中指出,您已将您的模型命名为Record
(我不确定这是否是拼写错误)
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
需要注意的另一点是Record
是Rails
中的保留字,因此使用Record
作为模型名称和相应的表records
通常会导致意外行为,例如创建多个记录。您应该避免将其用作模型(Record
)和表名(records
)。我建议您将Record
的型号名称更改为其他有意义的名称。
供您参考,您可以查看以下链接:Rails reserved words list: records