Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: Submitting an array in a form

I have a model that has an attribute that is an Array. What's the proper way for me to populate that attribute from a form submission?

I know having a form input with a field whose name includes brackets creates a hash from the input. Should I just be taking that and stepping through it in the controller to massage it into an array?

Example to make it less abstract:

class Article   serialize :links, Array end 

The links variable takes the form of a an array of URLs, i.e. [["http://www.google.com"], ["http://stackoverflow.com"]]

When I use something like the following in my form, it creates a hash:

<%= hidden_field_tag "article[links][#{url}]", :track, :value => nil %> 

The resultant hash looks like this:

"links" => {"http://www.google.com" => "", "http://stackoverflow.com" => ""} 

If I don't include the url in the name of the link, additional values clobber each other:

<%= hidden_field_tag "article[links]", :track, :value => url %> 

The result looks like this: "links" => "http://stackoverflow.com"

like image 494
William Jones Avatar asked Jun 22 '10 02:06

William Jones


People also ask

How do you push an element to an array in Ruby?

The push() function in Ruby is used to push the given element at the end of the given array and returns the array itself with the pushed elements. Parameters: Elements : These are the elements which are to be added at the end of the given array. Returns: the array of pushed element.

Can you print an array in Ruby?

Ruby printing array contentsThe array as a parameter to the puts or print method is the simplest way to print the contents of the array. Each element is printed on a separate line. Using the inspect method, the output is more readable. The line prints the string representation of the array to the terminal.

Can you add arrays in Ruby?

This can be done in a few ways in Ruby. The first is the plus operator. This will append one array to the end of another, creating a third array with the elements of both. Alternatively, use the concat method (the + operator and concat method are functionally equivalent).


2 Answers

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.

like image 156
Larry K Avatar answered Oct 05 '22 07:10

Larry K


= simple_form_for @article do |f|   = f.input_field :name, multiple: true   = f.input_field :name, multiple: true   = f.submit 
like image 36
stAndrei Avatar answered Oct 05 '22 07:10

stAndrei