Aditya Tiwari
Aditya Tiwari

Reputation: 209

Convert string into integer in rails

I'm creating a form_for in which one of the field fetches the drop-down list from the database. I'm interpolating the data to display the string but I want to store its id back into other database which is linked with my form.


class FlightsController < ApplicationController
  def new
    @flight = Flight.new
    @airplane = @flight.airplane
    @options = Airport.list
  end

  def create
    @flight = Flight.new(flight_params)
    if @flight.save!
      flash[:success] = "Flight created successfully."
      redirect_to @flight
    else
      flash[:danger] = "Flight not created."
      redirect_to :new
    end
  end

  private

    def flight_params
      params.require(:flight).permit(:name, :origin, :destination, :depart, :arrive, :fare, :airplane_id)
    end
end

<%= form_for(@flight) do |f| %>
  ...
  <div class="row">
    <div class="form-group col-md-6">
      <%= f.label :origin %>
      <%= f.select :origin, grouped_options_for_select(@options), { include_blank: "Any", class: "form-control selectpicker", data: { "live-search": true } } %>
    </div>
  </div>
...
<% end %>

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

The following error is showing,

Airport(#69813853361360) expected, got "43" which is an instance of String(#47256130076180)


The output of Airport.list when run in a console is shown below:

=> {"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"}

I tried using to_i but it didn't work.

Upvotes: 0

Views: 1852

Answers (4)

SteveTurczyn
SteveTurczyn

Reputation: 36880

Your problem is not integer versus string, your problem is (and the error is telling you) the field is expecting an Airport object, but it's getting an airport id...

<%= f.select :origin, grouped_options_for_select(@options), { include_blank: "Any", class: "form-control selectpicker", data: { "live-search": true } } %>

You're trying to select origin which is an airport object. You really are just returning the ID of an airport object (origin_id).

Change it to

<%= f.select :origin_id, grouped_options_for_select(@options), { include_blank: "Any", class: "form-control selectpicker", data: { "live-search": true } } %><%= f.select :origin, grouped_options_for_select(@options), { include_blank: "Any", class: "form-control selectpicker", data: { "live-search": true } } %>

And don't forget to update your flight_params

def flight_params
  params.require(:flight).permit(:name, :origin_id, :destination, :depart, :arrive, :fare, :airplane_id)
end

I would guess you'll have a similar problem with destination

Upvotes: 0

VENKATESH KARNI
VENKATESH KARNI

Reputation: 66

Convert string into integer in rails:

user_id = "123"

@user_id = user_id.to_i

puts @user_id

@user_id = 123

Convert integer into string in rails:

user_id = 456

@user_id = user_id.to_s

puts @user_id

@user_id = "456"

Convert column type string into integer in rails migration :

def change
  change_column :webinars, :user_id, :integer, using: 'user_id::integer'
end

Convert column type integer into string in rails migration:

 def change
   change_column :webinars, :user_id, :string, using: 'user_id::string'
 end

Upvotes: 0

Saqib Shahzad
Saqib Shahzad

Reputation: 1002

grouped_options_for_select is sending a.id as string value. Convert it to integer in your create action.

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

Upvotes: 0

uday
uday

Reputation: 1481

if you're interpolating a string with space delimiter you can try this.

'1 one'.split(' ').first.to_i

Upvotes: 1

Related Questions