0

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.

7
  • what you see in a params in console ? Commented Feb 11, 2019 at 12:35
  • 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"} Commented Feb 11, 2019 at 12:57
  • origin and destination have string format in params but database column is of type bigint. Commented Feb 11, 2019 at 12:59
  • 1
    try replace origin to origin_id on your f.select and update a params Commented Feb 11, 2019 at 13:03
  • It gives the error - Origin must exist, Destination must exist. I think it is because they are the foreign keys from Airport. Commented Feb 11, 2019 at 13:08

4 Answers 4

1

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

'1 one'.split(' ').first.to_i
Sign up to request clarification or add additional context in comments.

11 Comments

No, actually my drop-down shows the list of airports but when I submit the form it should store its id in integer format instead it is returning id in string format hence couldn't store it
instead of using grouped_options_for_select try this options_for_select for displaying dropdown values in view
flight_params are listed in the controller under private
i know that! I requested you to upload those params data when you're creating
I can't use options_for_select because I need to group multiple data and display it as a single record.
|
0

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!
    ...

1 Comment

Bro, I tried this already (as mentioned in the question) but it is not working.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.