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.
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.
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?
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']")