在AJAX调用后在rails中呈现部分时未定义的表单变量

时间:2013-10-15 06:54:04

标签: javascript jquery ruby-on-rails ajax forms

我一直试图在rails 4.0中使用AJAX实现动态依赖表单。我有一个bike模型has_one makemodel。它还has_many quotes。目标是在选择第一个字段model后填充第二个表单的集合字段(make)。

表单正确加载并在选择第一个字段后调用AJAX并返回相应的数据集(通过控制台中的puts确认)。但是,我无法弄清楚如何适当地渲染部分。我收到以下错误。我知道,一旦通过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 %>

在引号控制器中:

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

in 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)

1 个答案:

答案 0 :(得分:0)

我通过将'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 %>