簡體   English   中英

在2個對象上使用表格/表格的問題

[英]problem with using form / formtastic on 2 objects

我確實很難完成這項工作。 我有一個模型appointment和一個模型appointment_block subscription_blocks表將開始時間和結束時間另存為datetime。 我正在使用一種稱為split_time_block的方法,該方法將split_time_block范圍分成15分鍾的數據包,並以字符串數組形式返回。 這很好。 我可以在選擇按鈕中選擇不同的時間。 appointments表引用約會_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

謝謝指教!!

正如您所發現的那樣,約會和約會塊在表單中沒有任何有意義的方式捆綁在一起。

假設您在模型中設置了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函數中實例化一個約會模型對象時,您應該能夠看到嵌套的對象:

@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.

形式化文檔(對於嵌套形式,請查看一半)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM