phil88530
phil88530

Reputation: 1509

formtastic one to many relationship

I have a model Driver which has many relationship to claims, I wish to have a form that client can enter their detail, together with a number of claims they've had in the past.

In the model, I have:

has_many :claims

and in the form I entered:

<%= semantic_form_for @driver do |f| %>
  <%= f.input :name %>
  .......

  <%= f.inputs :for => :claims do |c| %>
    <% c.input :happen_date %>
    <% c.input :claim_details %>
    ...........
  <% end %>
<% end %>

When I submit the form, the issue happens, I got Claim(#3213231311) expected, got Array(#70299228017580)

And I had a look at the form, I saw:

{"utf8"=>"✓",
"authenticity_token"=>"VD3lt+LXZoA94YoL3PkI0frTH5EzT4vs/lZhzQhp0IQ=",
"driver"=>{
"name"=> "test tester",
.......
"claims"=>{"happend_date(3i)"=>"10",
"claim_date(2i)"=>"10",
"claim_date(1i)"=>"2012",
"claim_details"=>"dsadsadsadas"}},
"commit"=>"Next"}

Since there is a has_many relationship with the claims, shouldn't claims be covered in an array like:

"claims" => [{.....},{......}]

I've been working so hard on the form and still couldn't get claims to be send as array, anyone help?

Upvotes: 0

Views: 145

Answers (1)

Rails Guy
Rails Guy

Reputation: 3866

Did you include this in your 'Driver' model:-

accepts_nested_attributes_for :claims

Edit your form like :

<%= semantic_form_for @driver do |f| %> 
  <%= f.input :name %>
  <%= f.semantic_fields_for :claims do |c| %>
    <% c.input :happen_date %>
    <% c.input :claim_details %>
  <% end %>
<% end %>

Hope it will help. Thanks

Upvotes: 2

Related Questions