Reputation: 557
An array filed saved in db as array .. every item in this array should be rendered as text_fields in a fields_for. (In my Rails 4 App with Postgres DB)
which is not rendered as expected ... please looking for you suggestions.
The Code is as follow:
Resource Migration have:
t.string :news_outlets, array: true
_form.html of parent Post
<%= form_for [@post, @post.declare_victory], :html => {} do |f| %>
<%= f.hidden_field :post_id, value: @post.id %>
<%= f.fields_for :news_outlets do |news_outlet| %>
<%= render 'news_outlet_fields', f: news_outlet %>
<div>
<%= link_to_add_item t('add_news'), news_outlet, '/news_outlet_fields', 'news_outlets'%>
</div>
<% end %>
<% end %>
Controller Action in DeclareVictory
def create
@declare_victory = @post.build_declare_victory declare_victory_params
@declare_victory.user = current_user
@declare_victory.save
redirect_to @post, notice: t('success')
end
_news_outlet_fields.html partial to be rendered
<div class="form-group">
<%= f.text_field '', id: 'declare_victory_news_outlets', :class => 'form-control', :placeholder => t('news_outlets.new_news') %>
</div>
Thanks
Upvotes: 4
Views: 318
Reputation: 18037
The text_field
needs to include the attribute name for mass assignment to work.
<%= f.text_field :news_outlets, :class => 'form-control', :placeholder => t('news_outlets.new_news') %>
The way you had it, the name
attribute wouldn't have been right so the params wouldn't have been right so the mass assignment attempt wouldn't have picked up on the new assignment of data from the form.
Upvotes: 1