1

I'm trying to take in an array of numbers through the use of a form. In my database I have the variable as:

t.integer  "home_goal_min", default: [],  array: true

In my form I have:

<%= f.label :minutes_of_home_team_goals %>
<%= f.fields_for 'home_goal_min[]', [] do |p| %>
    <%= f.number_field :home_goal_min %>
    <%= f.number_field :home_goal_min %>
<% end %>

In my controller I also added in the parameter as an array but this still hasn't solved my problem:

def result_params
        params.require(:result).permit(:home_result, :away_result, {:home_goal_min => []}, {:away_goal_min => []})
    end

However, when I use the form and enter data, I then proceed to check the database through the console but it still appears empty and I just get: home_goal_min: []

I'm wondering what I need to do to get the numbers entered in the form to be saved in the database?

Also is there a quick way to have the form part for home_goal_min as a text field and allow the user to enter the numbers split by comma, for example as: "23,45,52" would populate home_goal_min with the array [23,45,52]

2 Answers 2

1

You have <%= f.number_field :home_goal_min %>

Shouldn't it be <%= p.number_field :home_goal_min %>?

Edit:

I don't think you can submit Arrays through forms without using javascript. Here's the simplest solution:

In the form:

<%= f.text_field :home_goal_mins_list, placeholder: "A comma-separated list of times of the goals" %>

In the model:

def home_goal_mins_list=(value)
  self.home_goal_mins = value.split(",").map(&:strip)
end

def home_goal_mins_list
  self.home_goal_mins.map(&:to_s).join(", ")
end

HOWEVER

If I were you, I would just make this data into its own table. Generally, it's a bad practice to use array fields unless your database is already storing a lot of unstructured data

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

4 Comments

well I have my form as <%= form_for ([@league, @team, @fixture, @result]) do |f| %> so it should be using it as f.number_field ?
Nope, within a fields_for block, you should be using the field object created in the block. Otherwise, rails has no way of knowing that the field inside that block is supposed to be in the fields_for the attribute you're modifying
ok yeah that makes sense. When I change it to use p.number_field I'm getting an error undefined method 'home_goal_min' for []:Array. Am I using the home_goal_min the right way ?
That's because the names you give fields are the names of attributes rails tries to set on the object, so rails is trying to call `.home_goal_min=' on an array, which isn't possible. If I were you, I'd ditch the nested fields and enter the data in a single field then parse is in the model. Currently updating my answer to explain
1

Firstly, you can pass in array through a form. It is done by appending [] to the end of the name of the inputs. For example, your form contains:

<input name='home_goal_min[]' value='100'>
<input name='home_goal_min[]' value='200'>
<input name='home_goal_min[]' value='300'>

Upon form submission, your params will look like:

params[:home_goal_min] => ['100', '200', '300']

Docs here: http://guides.rubyonrails.org/v3.2.9/form_helpers.html Section 7.1 Basic Structures

However, reading through your situation, I don't think you need to pass in an array. As Ben noted in his answer, you can parse a single field before saving it. I would suggest doing it in the form object, as model has nothing to do with parsing form data.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.