Ruby on rails 在rails中将字符串转换为整数

Ruby on rails 在rails中将字符串转换为整数,ruby-on-rails,ruby-on-rails-5,form-for,Ruby On Rails,Ruby On Rails 5,Form For,我正在为其中一个字段创建一个表单,其中一个字段从数据库中获取下拉列表。我正在插值数据以显示字符串,但我想将其id存储回与我的表单链接的其他数据库中 我试着用它,但没用 如果要使用空格分隔符插入字符串,可以尝试此操作 '1 one'.split(' ').first.to_i \u select的分组\u选项\u正在将.id作为字符串值发送。在创建操作中将其转换为整数 def create @flight = Flight.new(flight_params) @flight.o

我正在为其中一个字段创建一个表单,其中一个字段从数据库中获取下拉列表。我正在插值数据以显示字符串,但我想将其id存储回与我的表单链接的其他数据库中


我试着用它,但没用

如果要使用空格分隔符插入字符串,可以尝试此操作

'1 one'.split(' ').first.to_i
\u select的分组\u选项\u正在将.id作为字符串值发送。在创建操作中将其转换为整数

def create
    @flight = Flight.new(flight_params)
    @flight.origin = @flight.origin.to_i  ## <== add this line
    if @flight.save!
    ...

不实际上,我的下拉列表显示了机场列表,但当我提交表单时,它应该以整数格式存储其id,而不是以字符串格式返回id,因此无法存储它,而不是使用分组的\u选项\u用于选择尝试此选项\u用于选择用于在视图中显示下拉值飞行\u参数列在下的控制器中Private我知道!我请求您在创建时上载这些参数数据。我无法将选项“”用于“”选择,因为我需要将多个数据分组并将其显示为单个记录。您在控制台中的参数中看到了什么?参数:{utf8=>✓, 真实性令牌=>+Z8+RKRJKKGATZNWYTD/QjEoq3kR4ZmoUTp+EpM+320fNFg5rJm+Izx1zBODo/H7IIm3D+yg3ysnVUPmy7ZwQ==,航班=>{name=>Indigo,起点=>49,目的地=>11,出发=>2019-02-21T21:30,到达=>2019-02-22T01:30,票价=>2500,飞机id=>3},提交=>源和目标在参数中具有字符串格式,但数据库列的类型为bigint。请尝试在f上将源替换为源。\u id。选择并更新参数。如果出现错误-源必须存在,目标必须存在。我想这是因为它们是机场的外键。兄弟,正如问题中提到的,我已经试过了,但不起作用。
class Airport < ApplicationRecord
  def self.list
    grouped_list = {}
    includes(:country).order("countries.name", :name).each do |a|
      grouped_list[a.country.name] ||= [["#{a.country.iso} #{a.country.name}", a.country.iso]]
      grouped_list[a.country.name] << ["#{a.iata} #{a.name} (#{a.city}, #{a.country.name})", a.id]
    end
    grouped_list
  end
end
class Flight < ApplicationRecord
  belongs_to :origin, class_name: "Airport"
  belongs_to :destination, class_name: "Airport"
  belongs_to :airplane
  has_many :bookings, dependent: :destroy
  has_many :passengers, through: :bookings
end
=> {"India"=>[["IN India", "IN"], ["AGX Agatti Airport (Agatti, India)", 3], ["IXV Along Airport (Along, India)", 5], ["AML Aranmula International Airport (Aranmula, India)", 6], ["IXB Bagdogra International Airport (Siliguri, India)", 50]]}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+Z8+rkrJkkgaTznnwyTd/QjEoq3kR4ZmoUTp+EpM+320fNFg5rJm+Izx1zBODo/H7IIm3D+yg3ysnVUPmy7ZwQ==", "flight"=>{"name"=>"Indigo", "origin"=>"49", "destination"=>"11", "depart"=>"2019-02-21T21:30", "arrive"=>"2019-02-22T01:30", "fare"=>"2500", "airplane_id"=>"3"}, "commit"=>"Create Flight"}
'1 one'.split(' ').first.to_i
def create
    @flight = Flight.new(flight_params)
    @flight.origin = @flight.origin.to_i  ## <== add this line
    if @flight.save!
    ...