7

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?

1

4 Answers 4

6

You still need a fields_for in your view, just use :relationships as the record_name then provide an object.

<%= form_for @account do |f| %>
    <%= f.text_field :name %>

    <% fields_for :relationships, @eligible_parents do |p| %>
        <%= p.check_box "relationships", nil, :value => p.object.id  %>
        <b><%= p.object.name %></b><br/>
    <% end %>

    <%= f.submit "Submit" %>
<% end %>

Documentation here: ActionView::Helpers::FormHelper

Sign up to request clarification or add additional context in comments.

2 Comments

thanks! where do i learn more about these! Neither rails guides, nor popular books talk about this kind of special usages.
I found this out while trying to solve one of my problems, this particular thing I ended up looking at documentation and the Rails source on github, I ended up finding little gems like this.
3

If you want to send array of values just use [] in name attributes.In your case just use

<%= f.check_box "relationships", {}, :value => p.id, :name => "relationships[]"   %>

4 Comments

undefined method `merge' for nil:NilClass
<%= check_box_tag 'ip_address[]', ip_res.ip_address, false %> ip_address is attr of my model IpResource
Its my previous code.. after porting it into format like your answer, I got above undefined method 'merge' for nil:NilClass error.
2

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:

<%= form_for :team do |t| %>
  <%= t.fields_for 'people[]', [] do |p| %>
    First Name: <%= p.text_field :first_name %>
    Last Name: <%= p.text_field :last_name %>
  <% end %>
<% end %>

your params data should return like this:

"team" => {
  "people" => [
    {"first_name" => "Michael", "last_name" => "Jordan"},
    {"first_name" => "Steve", "last_name" => "Jobs"},
    {"first_name" => "Barack", "last_name" => "Obama"}
  ]
}

2 Comments

im trying to do this with an array of non-active record models but it keeps asking for the "id" and giving me "undefined method id"
@dtc you are probably doing a .id which should be ['id'] if you're working with non-active record models
0

I'm working in something similar. My issue was how to pass the input name to a partial to resolve it as a hash using the rails form_for helper.

So, my solutions was something like this:

= render 'my_partial', form: form, name: "item_ids[item.id]"

So, this gonna render an html like this:

<input class="form-control" name="products[items_ids[1]]" id="products_items_ids[1]">

The number 1 is the item.id, this is how HTML understand that you gonna pass an array or hash, all depends your config.

So, the params gonna look like this:

"items_ids"=>{"1"=>"1"}

This is working for me with an form_object + virtus

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.