gpt4 book ai didi

javascript - AJAX 调用后在 Rails 中渲染部分时未定义的表单变量

转载 作者:行者123 更新时间:2023-11-28 08:49:03 25 4
gpt4 key购买 nike

我已经尝试在rails 4.0中使用AJAX实现动态依赖表单有一段时间了。我有一个 bike 模型,其中 has_one makemodel。它还has_many 引号。目标是在选择第一个字段 make 后填充第二个表单的集合字段 (model)。

表单正确加载,一旦选择第一个字段,就会进行 AJAX 调用,并返回适当的数据集(通过控制台中的放入确认)。但是,我无法弄清楚如何正确渲染部分。我收到以下错误。我知道一旦通过 AJAX 调用呈现部分就没有 f 变量,但是如何在没有 f 变量的情况下设计表单?

ActionView::Template::Error (undefined local variable or method `f' for #<#<Class:0x007f95b0fc2068>:0x007f95b54e2490>):
1: <%= f.label :name, 'Model' %>
2: <% if !current_models.blank? %>
3: <%= f.collection_select :name, current_models.collect{ |m| [m.name,m.id]}, :id , :name, include_blank: true %>
4: <% else %>
app/views/shared/_model_questions_fields.html.erb:1:in `_app_views_shared__model_questions_fields_html_erb__894239665582553972_70140482609500'
app/controllers/quotes_controller.rb:70:in `update_model_select'

引用/new.html.erb

<%= form_for @quote do |f| %>
<%= f.fields_for :bikes do |builder| %>
<p><%= render 'shared/bike_questions_fields', :f => builder %></p>
<% end %>
<%= f.submit "Show Quote", class: "btn btn-large btn-primary" %>
<% end %>

“共享/bike_questions_fields”

<%= f.fields_for :makes do |builder| %>
<p><%= render 'shared/make_questions_fields', :f => builder %></p>
<% end %>

<%= f.fields_for :models do |builder| %>
<div id="bikeModels"
<p><%= render 'shared/model_questions_fields', :f => builder, :current_models => [] %></p>
</div>
<% end %>

“共享/model_questions_fields”

<%= f.label :name, 'Model' %>
<% if !current_models.blank? %>
<%= f.collection_select :name, current_models.collect{ |m| [m.name,m.id]}, :id , :name, include_blank: true %>
<% else %>
<%= f.collection_select :name, [], :id , :name, include_blank: true %>
<% end %>

引号 Controller :

def update_model_select
models = Model.where(:make_id=>params[:id]).order(:name) unless params[:id].blank?
render :partial => "shared/model_questions_fields", :locals => { :current_models => models }
end

在quotes.js.coffee中:

ready = ->
jQuery ($) ->

# when the #make field changes
$("#quote_bikes_makes_name").change ->

# make a POST call and replace the content
make = $("select#quote_bikes_makes_name :selected").val()
make = "0" if make is ""
jQuery.get "/quotes/update_model_select/" + make, (data) ->
$("#bikeModels").html data

false


$(document).ready(ready)
$(document).on('page:load', ready)

最佳答案

我通过将'shared/model_questions_fields'更改为:

解决了这个问题
<%= form_for('quote', remote: true) do |f| %>
<%= f.fields_for :bikes do |g| %>
<%= g.fields_for :models do |i| %>
<%= i.label :name, 'Model' %>
<% if !current_models.blank? %>
<%= i.collection_select :name, current_models, :id , :name, include_blank: true %>
<% else %>
<%= i.collection_select :name, [], :id , :name, include_blank: true %>
<% end %>
<% end %>
<% end %>
<% end %>

关于javascript - AJAX 调用后在 Rails 中渲染部分时未定义的表单变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19374956/

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