lundi 1 juin 2015

Enable and Populate new fields on Select

Ok on my site i have a place where you can select the year make and model of a vehicle.

<section style="margin-bottom: 0;" class="auto-container fields">
  <div class="row">
    <div class="col col-md-2">
      <label class="select">
        <%= f.select :year, options_for_select((1900..Date.today.year+1).to_a.reverse, f.object.year), {:prompt => 'Year', :class=>"pointer" } %>
      </label>
    </div>
    <div class="col col-md-3">
      <label class="input">
        <%= f.text_field :make, :placeholder => "Make" %>
      </label>
    </div>
    <div class="col col-md-3">
      <label class="input">
        <%= f.text_field :model, :placeholder => "Model" %>
      </label>
    </div>
    <div class="col col-md-3">
      <label class="input">
        <%= f.text_field :style, :placeholder => "Style" %>
      </label>
    </div>
    <div class="col col-md-2">
      <p class="btn btn-primary pull-right" style="margin: 0 0 0 20px"><%= button_to_remove_fields "Remove", f %></p>
    </div>
  </div>
</section>

I want to upgrade that to use the edmunds_gem that will populate each of the make model and style of the vehicle but i want to keep the other fields disabled until a selection is made and then populate the next

when form is first generated:

Should only have year enabled

then:

After year selected should enable makes and populate makes with this but pass in the year that was selected.

  <%= f.select :make, options_for_select(CarDetails.get_makes(year)), :class=>"pointer" %>

After make is selected should enable models and populate models with this but pass in the year and make that was selected.

  <%= f.select :models, options_for_select(CarDetails.get_models(year, make)), :class=>"pointer" %>

After model is selected should enable styles and populate styles with this but pass in the year, make, and model that was selected.

  <%= f.select :models, options_for_select(CarDetails.get_styles(year, make, model)), :class=>"pointer" %>

this is my CarDetails Class

class CarDetails
  def self.get_makes(year)
    makes = Edmunds::Make.new.find_makes_by_model_year(year)
    ActiveSupport::JSON.decode(makes.to_json).map { |m| [m["name"], m["niceName"]] }
  end

  def self.get_models(year, make)
    models = Edmunds::Model.new.find_models_by_make_and_year(make, year)
    ActiveSupport::JSON.decode(models.to_json).map { |m| [m["name"], m["niceName"]] }
  end

  def self.get_styles(year, make, model)
    styles = Edmunds::Style.new.find_styles_by_make_model_year(make, model, year)
    ActiveSupport::JSON.decode(styles.to_json).map { |m| [m["trim"]["name"], m["id"]] }
  end

  def self.get_weight(style_id)
    specs = Edmunds::Style.new.find_by_id(style_id)
    #weight
    ActiveSupport::JSON.decode(specs.to_json).map { |m| m["attributeGroups"]["SPECIFICATIONS"]["attributes"]["CURB_WEIGHT"]["value"] }
    # dimensions
       #length
    ActiveSupport::JSON.decode(specs.to_json).map { |m| m["attributeGroups"]["EXTERIOR_DIMENSIONS"]["attributes"]["OVERALL_LENGTH"]["value"] }
       #width
    ActiveSupport::JSON.decode(specs.to_json).map { |m| m["attributeGroups"]["EXTERIOR_DIMENSIONS"]["attributes"]["OVERALL_WIDTH_WITH_MIRRORS"]["value"] }
  end
end

ONE MORE IMPORTANT THING

this form has to be able to use the Nesting. as it is i can dynamically add and remove additional auto fields.

enter image description here

Aucun commentaire:

Enregistrer un commentaire