ruby on rails - How to properly display value in edit form while using bootstrap3_autocomplete_input and association -
i trying use auto-complete/type-ahead feature provided following rails gem https://github.com/maxivak/bootstrap3_autocomplete_input , https://github.com/plataformatec/simple_form
everything works fine in case choose "new" action new record in form. able choose value in input field auto-complete feature. problem in case choose "edit" action edit existing record. in case field not show correct value (pre-filled form), shows like: #<airport:0x007f98b478b7a8>
even in "show" action, can see correct value displayed.
i tried change f.input f.association had before started implementing auto-complete, did not helped.
records in cargo model have correct airports_id reference stored, checked manually in rails console.
question how can correct airport value pre-filled form in case choose "edit" action, instead kind of reference, got.
rails 4.1.7
my code is:
cargo model:
class cargo < activerecord::base belongs_to :airport ...
cargo view:
... <%= f.input :airport, :as => :autocomplete, :source_query => autocomplete_airport_city_airports_url %> ...
airport model:
class airport < activerecord::base has_many :cargos, :dependent => :destroy attr_accessible :iata_code, :name, :city validates :iata_code, :name, :city, presence: true validates :iata_code, :uniqueness => { :scope => :name } validates :iata_code, length: { is: 3 }, format: { with: /\a[a-za-z\d\s]*\z/ } validates :name, :city, length: { minimum: 2, maximum: 128 } def full_airport_name "#{city} / #{iata_code}" end end
airports controller
class airportscontroller < applicationcontroller autocomplete :airport, :city, { :display_value => 'full_airport_name', :full_model=>true } ...
routes:
resources :airports :autocomplete_airport_city, :on => :collection end
actually found problem. first of refactored airports model
, removed columns name
, , reseed name column data concatenated separate strings iata code / city
. after this, there need specify in model, show value. solved issue:
class airport < activerecord::base has_many :cargos, :dependent => :destroy attr_accessible :name validates :name, presence: true validates :name, :uniqueness => true def to_s name end end
this described, didnot understand on first sight previously, in original documentation here https://github.com/maxivak/bootstrap3_autocomplete_input section model.
Comments
Post a Comment