0

strong textI'm trying to make dynamic select boxes via "data-remote attribute for select boxes". In console it seems that i'm getting right parameters(the id of selected make), but I can't figure it out how to pass it to controller to get models with matching make_id:s.

Heres the attached_vehicles form part from _form.html.erb

<div class="vehicle_field">
    <%= f.fields_for :attached_vehicles do |av| %>
    <p>Select make</p>
    <%= av.select :make, (@makes.collect { |m| [m.make_name, m.id] }), { include_blank: "Select make" }, { data: { remote: true, url: "update_make_models", name: "make", update: "#diy_attached_vehicles_attributes_0_model"} } %><br>
    <p>Select model</p>
    <%= av.collection_select :model, @models, (render "make_models/make_model"), { prompt: "Select model" } %><br>
    ...
    <% end %>
</div>

../views/diys/update_make_models.coffee

$.empty()
    .append("<%= escape_javascript(render "make_models/make_model") %>")

../diys_controller.rb

...
def update_make_models
  @models = MakeModel.where("make_id = ?", params[:make])
end

def new
  @diy = Diy.new
  @step = @diy.steps.new
  @attached_vehicle = @diy.attached_vehicles.new
  @step.add_images_to_steps.new
  @makes = Make.all
  @models = MakeModel.where("make_id = ?", params[:make_id])
end
...

../views/make_models/_make_model.html.erb

<% @models.collect do |models| %>
   <option value="<%= models.id %>"><%= models.make_model_name %></option>
<% end %>

And here's what i'm getting in console after selecting make in makes select box

Started GET "/diys/update_make_models?diy%5Battached_vehicles_attributes%5D%5B0%5D%5Bmake%5D=12" for ::1 at 2016-02-18 20:22:35 +0200 Processing by DiysController#update_make_models as JS
  Parameters: {"diy"=>{"attached_vehicles_attributes"=>{"0"=>{"make"=>"12"}}}}
  MakeModel Load (1.0ms)  SELECT "make_models".* FROM "make_models" WHERE (make_id = NULL)
  Rendered make_models/_make_model.html.erb (3.0ms)
  Rendered diys/update_make_models.coffee (491.0ms)
Completed 200 OK in 628ms (Views: 626.5ms | ActiveRecord: 1.0ms | Solr: 0.0ms)

------------------------------------------------------------------------------------------------------------------------------------ Edit

NameError (undefined local variable or method `attached_vehicles_attributes' for #<DiysController:0x5757648>):
  app/controllers/diys_controller.rb:28:in `update_make_models'

1 Answer 1

1

Your params hash is(according to logs): {"diy"=>{"attached_vehicles_attributes"=>{"0"=>{"make"=>"12"}}}}. So, if you want to get :make_id from it, you should write:

def update_make_models
  @models = MakeModel.where(make_id: params["diy"]["attached_vehicles_attributes"]["0"]["make"])
end
Sign up to request clarification or add additional context in comments.

1 Comment

Now it gives me this error in console after selecting make NameError (undefined local variable or method attached_vehicles_attributes' for #<DiysController:0x5757648>): app/controllers/diys_controller.rb:28:in update_make_models'

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.