ruby on rails - Simple form text input fields as part of an array -
so have 3 text input fields , each 1 represents co-ordinate , stored in array format [x, y, z]
in model.
i trying use input fields work produce array submitted form. code currently:
=f.input_field :coordinates[0], value: "1" =f.input_field :coordinates[1], value: "2" =f.input_field :coordinates[2], value: "3"
so hoping can use coordinates
param in controller save database. issue setup html produced <input value="1" name="shape[o]" id="shape_o">
when should <input value="1" name="shape[coordinates][]" id="shape_coordinates_0">
n.b. have serialize :coordinates
in model
try set custom attributes directly this:
= f.input_field :coordinates, input_html: { value: 1, id: "shape_coordinates_0", name: "shape[coordinates][]" }
but suggest create attr_readers
in model each coordinate , unite in array:
# model: class shape < activerecord::base attr_reader :x, :y, :z #since want serialize before_create :build_coordinates private def build_coordinates self.coordinates = [x, y, z] end end
in case view easy like:
=f.input_field :x, value: "1" =f.input_field :y, value: "2" =f.input_field :z, value: "3"
Comments
Post a Comment