Get an Array from a Rails Form

Ruby on Rails: Submitting an array in a form

If your html form has input fields with empty square brackets, then they will be turned into an array inside params in the controller.

# Eg multiple input fields all with the same name:
<input type="textbox" name="course[track_codes][]" ...>

# will become the Array
params["course"]["track_codes"]
# with an element for each of the input fields with the same name

Added:

Note that the rails helpers are not setup to do the array trick auto-magically. So you may have to create the name attributes manually. Also, checkboxes have their own issues if using the rails helpers since the checkbox helpers create additional hidden fields to handle the unchecked case.

Get an array from a Rails form

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

Return an array using `form_with`?

You should be able to do this like so:

<%= form_with model: @dog, local: true do |my_form| %>
<% ['labrador','husky'].each do |breed| %>
<%= my_form.check_box :breeds, {multiple: true}, breed%>
<%= my_form.label :breeds, breed.titleize %>
<%end%>
<%= my_form.submit 'Save' %>
<% end %>

The signature for check_box is (object_name, method, options = {}, checked_value = "1", unchecked_value = "0").

Since you are using the form builder the object_name can be omitted as it will be my_form.

So we pass:

  • the method :breeds,
  • the options {multiple: true} so that it creates a collection
  • the checked_value will be the breed name.

ActionView::Helpers::FormHelper#check_box Documentation

Rails form_for pass params as Array

try this

= form_for :question, :url => questions_path do |f|
= f.text_field :title, name: "question[title][]"

for more info refer http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters

Submit form with an array input - Rails

Looks like you may need another pair of brackets, try this:

<%= f.select :access, options_for_select(custodian_profiles), {}, {:multiple => true, :class => "form-control #{error_class}", include_blank: true } %>

Also make sure that your custodian_profiles variable is returning an array. Hope that helps.

How to submit a string array in a form Rails 4.2

It is returning a single string because the input is a text field. If you want to utilize a text field for this then just expect that the input will be comma-separated and split the input:

@message.recipients = params[:message][:recipients].split(',')

# or, if it's wrapped in an array
# @message.recipients = params[:message][:recipients][0].split(',')

Rails, array of objects form

So first in your controller

def my_action
@datas = MyData.find(2, 5, 7)
end

Then in your view

You need to iterate through the @datas array and yield the fields for each object. That is because fields_for yields fields for one object only, not arrays of objects.

<%= form_tag 'myOp' do |f|%>
<% @datas.each_with_index do |data, i| %>
<%= fields_for "test_#{i}", data do |builder|%>
<%= render 'sub_form', :f => builder %>
<% end %>
<% end %>
<% end %>


Related Topics



Leave a reply



Submit