Muhambi
Muhambi

Reputation: 3522

Grab form data nested fields

I have a rails app that used nested fields via the Cocoon gem:

_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

What I need to do is retrieve each tasks description value on the edit page for the user.

Upvotes: 0

Views: 46

Answers (1)

Surya
Surya

Reputation: 16022

Not sure what you mean by echo, but to get the description of task in _task_fields.html.haml you can do this:

.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

Upvotes: 1

Related Questions