Home:ALL Converter>How can I make the select returning an integer value?

How can I make the select returning an integer value?

Ask Time:2018-06-08T22:08:36         Author:Proz1g

Json Formatter

This might be a stupid question but how can I make the select to return an integer?

I have this:

<%= select_tag :month, options_for_select([["01 | Janeiro", 1], ["02 | Fevereiro", 2], ["03 | Março", 3], ["04 | Abril", 4], ["05 | Maio", 5], ["06 | Junho", 6], ["07 | Julho", 7], ["08 | Agosto", 8], ["09 | Setembro", 9], ["10 | Outubro", 10], ["11 | Novembro", 11], ["12 | Dezembro", 12]]), {include_blank: true} %>

enter image description here

Controller:

 def create
   @vehicle = Vehicle.new(vehicle_params)
   if @vehicle.save
     redirect_to @vehicle
   else
     render 'new'
   end
 end

 def edit
 end

 def update
   if @vehicle.update(vehicle_params)
     redirect_to @vehicle
   else
     render 'edit'
   end
 end
 .
 .
 .
 def vehicle_params
   params.require(:vehicle).permit(:month, :year, :version, :kms, :power, :cc, :transmission, :fuel, :category, :seats, :doors, :color, :condition, :warranty, :price, :manufacturer_id, :model_id, {feature_ids: []}, :banner_image, images: [])
 end

Logs:

Started PATCH "/vehicles/6" for 10.0.2.2 at 2018-06-08 14:42:27 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by VehiclesController#update as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"lyBWt44ta0VoT3adk38Qc7zniWzwJqX3nv0MNIgiL58TVOaU3K0qaRbNNAsb60lPqtTwlQNjjnZV2rIfcv4xaw==", "vehicle"=>{"manufacturer_id"=>"7", "model_id"=>"30", "kms"=>"57528", "power"=>"116", "cc"=>"4499.0", "version"=>"458 Itália", "transmission"=>"Manual", "fuel"=>"Gásoleo", "category"=>"Coupé", "seats"=>"2", "doors"=>"2", "color"=>"Vermelho", "condition"=>"Usado", "warranty"=>"3", "price"=>"178900", "feature_ids"=>["", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]}, "month"=>"8", "year"=>"2010", "commit"=>"Submeter", "id"=>"6"}
  Vehicle Load (1.1ms)  SELECT  "vehicles".* FROM "vehicles" WHERE "vehicles"."id" = ? LIMIT ?  [["id", 6], ["LIMIT", 1]]
  ↳ app/controllers/vehicles_controller.rb:86
   (0.1ms)  begin transaction
  ↳ app/controllers/vehicles_controller.rb:72
  Feature Load (1.2ms)  SELECT "features".* FROM "features" WHERE "features"."id" IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["id", 1], ["id", 2], ["id", 3], ["id", 4], ["id", 5], ["id", 6], ["id", 7], ["id", 8], ["id", 9], ["id", 10], ["id", 11], ["id", 12], ["id", 13]]
  ↳ app/controllers/vehicles_controller.rb:72
  Feature Load (0.3ms)  SELECT "features".* FROM "features" INNER JOIN "features_vehicles" ON "features"."id" = "features_vehicles"."feature_id" WHERE "features_vehicles"."vehicle_id" = ?  [["vehicle_id", 6]]
  ↳ app/controllers/vehicles_controller.rb:72
  Model Load (0.2ms)  SELECT  "models".* FROM "models" WHERE "models"."id" = ? LIMIT ?  [["id", 30], ["LIMIT", 1]]
  ↳ app/controllers/vehicles_controller.rb:72
  Manufacturer Load (0.2ms)  SELECT  "manufacturers".* FROM "manufacturers" WHERE "manufacturers"."id" = ? LIMIT ?  [["id", 7], ["LIMIT", 1]]
  ↳ app/controllers/vehicles_controller.rb:72
   (0.1ms)  commit transaction
  ↳ app/controllers/vehicles_controller.rb:72
Redirected to http://localhost:3000/vehicles/6
Completed 200 OK in 23ms (ActiveRecord: 3.2ms)

I suppose the problem is that the :month is an integer and the value returned by the select is a string...because the value returned is not inserted in the column...so, how can I make the select returning a integer?

Also, if I do this it works just fine:

<%= f.number_field :month, min: 1, max: 12, maxlength: 2 %>

But what I really want is the way I have in the select. Hope you can help me!

Thank you!

Author:Proz1g,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762492/how-can-i-make-the-select-returning-an-integer-value
yy