I need to design a form for a account
resource. In that form, i need to collect some set of ids as an array in the params
hash in attribute called relationships
.
So the final params[account]
hash from the POST request should be like:
{:name => 'somename', :relationships => ["123", "23", "23445"]}
How shall I design the form_for
fields? I tried this, but didn't work:
<%= form_for @account do |f| %>
<%= f.text_field :name %>
<% @eligible_parents.each do |p| %>
<%= f.check_box "relationships", nil, :value => p.id %>
<b><%= p.name %></b><br/>
</span>
<% end %>
<%= f.submit "Submit" %>
<% end %>
Number of elements in @eligible_parents
varies every time.
relationships
is neither an association nor an attribute in account
model.
I have to use virtual attributes but I need to fill in an array from a form.
Please help. How can I do this?
In a complex form, with nested attributes, you can make use of the f.object_name helper. But beware of the syntax when doing interpolation. This is correct:
This is NOT correct:
It always trips me up.
If you want to send array of values just use [] in name attributes.In your case just use
You still need a
fields_for
in your view, just use:relationships
as therecord_name
then provide an object.Documentation here: ActionView::Helpers::FormHelper
I found this to be the cleanest way...
If you are working with straight data and want to send back an array without using any of these @objects:
your params data should return like this: