gpt4 book ai didi

javascript - 为实例变量传递嵌套参数

转载 作者:行者123 更新时间:2023-12-03 07:39:00 25 4
gpt4 key购买 nike

强文本我正在尝试通过 "data-remote attribute for select boxes" 制作动态选择框。在控制台中,我似乎获得了正确的参数(所选品牌的 id),但我不知道如何将其传递给 Controller ​​以获取具有匹配 make_id:s 的模型。

这是来自_form.html.erb的attached_vehicles表单部分

<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 %>

这是在 make 选择框中选择 make 后我在控制台中得到的内容

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)

-------------------------------------------------------- -------------------------------------------------- --------------------------------------------------编辑

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

最佳答案

您的 params 哈希值是(根据日志):{"diy"=>{"attached_vehicles_attributes"=>{"0"=>{"make"=>"12"}}}}。所以,如果你想从中获取 :make_id ,你应该写:

def update_make_models
@models = MakeModel.where(make_id: params["diy"]["attached_vehicles_attributes"]["0"]["make"])
end

关于javascript - 为实例变量传递嵌套参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35490517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com