抓取表单数据嵌套字段

时间:2014-10-27 03:35:37

标签: ruby-on-rails ruby cocoon-gem

我有一个通过Cocoon gem使用嵌套字段的rails应用程序:

_form.html.haml:

= form_for @project do |f|
   %h3 Tasks
   #tasks
      = f.fields_for :tasks do |task|
          = render 'task_fields', :f => task
      .links
          = link_to_add_association 'add task', f, :tasks
   = f.submit

_task_fields.html.haml:

.nested-fields
   .field
       = f.label :description
       %br
       = f.text_field :description
       **********HERE's WHERE I WANT TO ECHO OUT THE DESCRIPTION FOR THIS PARTICULAR TASK**
   .field
       = f.check_box :done
       = f.label :done
   = link_to_remove_association "remove task", f

我需要做的是在用户的编辑页面上检索每个任务描述值。

1 个答案:

答案 0 :(得分:1)

不确定echo的意思,但要在 _task_fields.html.haml 中获取description任务,您可以这样做:

.nested-fields
   .field
       = f.label :description
       %br
       = f.text_field :description
       = f.object.description # will show description if exists on form's object here.
   .field
       = f.check_box :done
       = f.label :done
   = link_to_remove_association "remove task", f