Nidhin S G
Nidhin S G

Reputation: 1695

Multiple Select Tag in Rails

Im trying to implement a multiple level drop down list in Rails I have three Tables in my DB.

vehicle_make.rb

class VehicleMake < ActiveRecord::Base
  validates_uniqueness_of :make
  has_many :appointments

end

vehicle_model.rb

class VehicleModel < ActiveRecord::Base
  validates_uniqueness_of :model
  has_many :appointments

end

vehicle_make_model.rb

class VehicleMakeModel < ActiveRecord::Base
  validates_uniqueness_of :vehicle_make_id, :scope => :vehicle_model_id
end

and im trying to implement a multiple dropdown list in appointments.html.rb on selecting the vehicle model only corresponding make should load..

<%= f.select :vehicle_make_id, options_for_select(vehicle_make.map {|s| [s.make, s.id]}, appointment.vehicle_make_id), {}, {class: "form-control"} %>

and in my js i have..

$('#appointment_vehicle_make_id').on('change', function() {
    var vehicle_make_id = this.value;
    $.ajax({
        url : '/appointments/update_models',
        type : 'GET',
        data : {
            make_id : vehicle_make_id
        },
        success : function(response) {
            console.log(response);
        }
    });
});

and this is my controller method.

  def update_models
    @vehicle_models = VehicleModel.all
    @model_ids = []
    @selected_vehicle_models = VehicleMakeModel.where(vehicle_make_id: params[:make_id]).order(:vehicle_model_id) unless params[:make_id].blank?
    @selected_vehicle_models.each do |t|
      @model_ids << t.vehicle_model_id
    end
    respond_to do |format|
      format.html { render layout: false }
      format.js
    end

end

I have html page named update_models.html.erb associated to the above action.

<%= select_tag :vehicle_model_id, options_for_select(@model_ids.map {|s| [s.model, s.first.id]}, nil), {}, {class: "form-control"} %>

I get an error in terminal saying

ActionView::Template::Error (wrong number of arguments (4 for 1..3)):
    1: <%= select_tag :vehicle_model_id, options_for_select(@model_ids.map {|s| [s.model, s.first.id]}, nil), {}, {class: "form-control"} %>

Im stuck here. I dont know how to proceed from here.. please help

Upvotes: 0

Views: 209

Answers (1)

RAJ
RAJ

Reputation: 9747

In your controller action update_models, you are trying to render js, so it's trying to find template named as update_models.js.erb.

You can try replacing your respond_to block with:

respond_to do |format|
  format.json { render :json => @model_ids }
end

Afterwards, you will need to parse this data in your ajax success callback

Upvotes: 1

Related Questions