0

The situation

I have a form partial with various input fields using the Simple Form Gem in my Rails app. One of the questions is a checkbox input field and the user can select multiple values with questions relating to diet and lifestyle (do you drink coffee, eat white bread?... etc). On the database the corresponding column (named 'tick') is an integer input field.

What I want to do

I would like the user to select as many checkboxes as necessary and for those options to be displayed in the 'Show' view once the user submits the form. So the show view might display a list of checked and unchecked boxes next to a list of questions or else display only those checkboxes that have been checked. Either would be suitable. I have a feeling that the solution might be to create an array of the answers somehow and display the results in the view, but so far I haven't been able to do this.

What actually happens

Once the user submits the form they are redirected to the 'show' view where the corresponding html output is simply "Tick" with no actual answer given. See html output below:

.
.
.
<p>
<strong>Tick:</strong>
</p>
.
.
.

The Form partial: _form.html.erb

<%= simple_form_for(@formulario) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if 
  f.object.errors[:base].present? %>
.
.
.
.
<%= f.input :tick,
    label:'Pon un tic en las Casillas si son aplicables en tu caso',
    collection: [
    ['padecido desorden alimentaria?', '1'],
    ['comes mucho lacteo o trigo?', '2'],
    ['te gusta comer/preparer comida?', '3'],
    ['compras comida ecológica?', '4'],
    ['añades sal a la comida?', '5'],
    ['tomas café o té descafeinado?', '6'],
    ['usas margarinas?', '7'],
    ['cocinas con aceites vegetales?', '8'],
    ['bebes agua filtrada?', '9'],
    ['has cambiado tu dieta ultimamente?', '10'],
    ['tomas té o café?', '11'],
    ['comes pan blanco?', '12'],
    ['comes mucho comida frita?', '13'],
    ['compras mucha comida para llevar?', '14'],
    ['comes mucha fruta y verdua fresca?  ', '15'],
    ['añades muchas salsas a la comida?', '16'],
    ['comes fuera a menudo?', '17'],
    ['cocinas para mas personas?', '18'],
    ['tienes apetito grande?', '19'],
    ['tu dieta es repetitiva?', '20'],
    ['comes pasta y arroz?', '21'],
    ['comes comida preparada?', '22'],
    ['usas el microondas?', '23'],
    ['eliges comida bajo en grasa?', '24'],
    ],
    as: :check_boxes %>
.
.
.
<% end %>

The Show view: show.html.erb

<p id="notice"><%= notice %></p>
.
.
.
<p>
  <strong>Tick:</strong>
  <%= @formulario.tick %>
</p>
.
.
.
<%= link_to 'Edit', edit_formulario_path(@formulario) %> |
<%= link_to 'Back', formularios_path %>

Is creating an array the way forward or is there a quicker solution to this?

1 Answer 1

1

You can serialize your tick attribute to store an array in you DB.

If tick attribute is an integer, run a migration to change it to string:

def change
  change_column :formularios, :tick, :string
end

Serialize the attribute inside the model:

class Formulario < ApplicationRecord
  serialize :tick, Array
end

And permit tick strong parameter as an array:

def formulario_params
  params.require(:formulario).permit(tick: [])
end

Now your model can store multiple "numbers" in tick, just change the display in show view (@formulario.tick now returns something like "['1', '2', '3']")

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

6 Comments

I think this is probably what i need to do. could you clarify how to implement these steps. what exactly do i need to type into the command line. would it be: "change_column :formularios, :tick, :string". i am also unsure what the serialize part means
i linked in my answer the documentation about serialize (apidock.com/rails/ActiveRecord/Base/serialize/class). To run a migration, just do rails generate migration change_column_tick_type_from_formularios and change the type to string as i wrote in my answer inside the migrate file created.
when i replace :tick with tick: [ ] in the params i get a syntax error. syntax error, unexpected ',', expecting =>
also do you know how i would change the view to show only the ticked values from the checkboxes?
also i am noticing from the console that the ":tick" attribute is saving an empty array even though i tick several checkboxes in the form and save. something is preventing the array from saving properly. any idea why?
|

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.