mercredi 30 décembre 2020

How to Save Multiple Checkbox Values to a string?

I use Rails 6.1 and the gem simple_form. I want save value of multiple checkbox in an object (string serialized or array). i don't find a simple solution.

table

    class CreateSpots < ActiveRecord::Migration[6.0]
  def change
    create_table :spots do |t|

      t.string :wind_direction

      t.timestamps
    end
  end
end

_form.html.erb

<%= simple_form_for (spot) do |f| %> 
  <%= f.input :wind_direction, as: :check_boxes, collection: Spot::DIRECTION %>
  <%= f.submit %>
<% end %>

model

  class Spot < ApplicationRecord
  DIRECTION = ['N','NW','W','SW','S','SE','E','NE']
  end

After submit the form, the output of wind_direction is Nil

ideally, i look for a string, i work with this format actually.

wind_direction: "[\"W\", \"NW\", \"N\", \"NE\", \"E\"]",

Thanks for any suggestions




Aucun commentaire:

Enregistrer un commentaire