我在完成这件事时遇到了问题。我有一个模型appointment
和一个模型appointment_block
。 appointment_blocks表将开始和结束时间保存为datetime。我正在使用一个名为split_time_block
的方法,它将15分钟数据包中的时间范围拆分并以字符串数组形式返回。这很好用。我可以在选择按钮中选择不同的时间。 appointments
表引用了appointment_block_id。使用我的表单,我想将block_id发送到我的新约会表条目。我只在此列中获得null
个条目。
= semantic_form_for(@appointment) do |f|
-f.inputs do
-@appointment_blocks.each do |form|
=f.input :date, ,:as => :select, :collection => form.split_time_block
= f.input :category, :collection => Category.all, :as => :select
= f.input :memo
- f.buttons do
= f.commit_button
我的约会_控制器包含:
@appointment = Appointment.new
@appointment_blocks = AppointmentBlock.all
建议!!
答案 0 :(得分:1)
约会和约会_block在你的表格中没有任何有意义的联系(正如你所发现的那样)。
假设您在模型中设置了accepts_nested_attributes_for:
semantic_form_for(@appointment) do |f|
f.inputs do
f.semantic_fields_for :appointment_block do |ab_form|
ab_form.input :date, ,:as => :select, :collection => split_time_block
end
f.input :category, :collection => Category.all, :as => :select
f.input :memo
f.buttons do
f.commit_button
end
您的表单应该沿着这些方向看。然后,当渲染视图时,检查标记,您应该看到元素ID和名称中内置的正确嵌套。当它被发送到您的控制器并在您的create function中实例化一个Appointment模型对象时,您应该能够看到您的嵌套对象:
@appointment = Appointment.new params[:appointment]
flash[:notice] = @appointment.appointment_block.inspect <-- you should be able to see that the objects are nested properly, and in the db the id's line up properly.
Formtastic documentation (look about halfway down for nested forms)