Moncho Chavez
Moncho Chavez

Reputation: 694

Nested forms on rails not displaying correctly on Edit

Hi im using Nested_forms gem for a app, everything is working fine.. Im following the documentation here ...

My form is saving data to database, i can create infinite number of extra fields as i require.

The only problem is when i want to populate the list for example to Edit, then i can´t populate again the list with all the values the user have previously selected, just the 1st value is there , the 2nds select box that should appear, appear transparent.. i leave an image , because english is not my lenguage y probably suck describing it

enter image description here

EDIT: I think the problem is on the loop , because first time when you submit it look like this..

enter image description here

And after saving, and lunching the form again to edit. this is what you get.

enter image description here

Here is the code in there.

<div id="nacionalidad">
 <%= f.fields_for :citizens do |citizen_form| %>

    <div>
      <%= citizen_form.label  :citizen, t('generales.citizen')  %>
      <%= citizen_form.select :country_id , Country.all.collect {|p| [ t("generales."+p.iso), p.id ] }.sort_by {|label,code| label}, { :include_blank => true } , { :class => 'pca33' } %>
      <div id="delerr"><%= citizen_form.link_to_remove t('generales.delete') %></div>
    </div>

  <% end %>

  <%= f.link_to_add t('generales.add'), :citizens %>
  </div>

And the model

class Citizen < ActiveRecord::Base
  attr_accessible  :country_id

  belongs_to :player
  belongs_to :country
end

Upvotes: 1

Views: 116

Answers (1)

manishie
manishie

Reputation: 5322

You might be going about this the wrong way. In my opinion it's much easier to use multiple-select fields and has_many relations. Then everything just works magically!

Form:

<%= select_tag :countries, options_from_collection_for_select(Country.all, 'id', 'name'), :multiple => true %>

Model:

class Citizen < ActiveRecord::Base
  attr_accessible  :country_id

  belongs_to :player
  has_many :countries
end

And then if you'd like, you can use another javascript library to make your multiselects more user-friendly:

Upvotes: 1

Related Questions