在 Rails 中使用 escape_javascript 渲染部分时出错?

Error while rendering partial using escape_javascript in Rails?

我的 Rails 应用程序中有一个 Search 选项卡显示此表单:

search/_form.html.erb

<%= form_for :anything, remote: true, html: { id: 'search_form' } do |f| %>
<div class="form-border">
  <div class="container-fluid"><!-- Row 2 -->
    <div class="row">
      <!-- 1. Add "Entity" multiselect dropdown to the form -->
      <div class="col-lg-4" id="userFormColumn2">
        <div class="form-group">
          <br/> <br/>
          <b><%= label_tag "Entity" %><br/></b>
          <%= select_tag(:search_entity, options_for_select(Account.uniq.pluck(:entity)), :multiple => true) %>
          <br/><br/>
        </div>

        <!-- 2. Add "Financial Institution" multiselect dropdown to the form -->
        <div class="col-lg-4" id="userFormColumn1">
          <div class="form-group">
            <%if(!@search_financial_institutions.blank?)%>
            <%= f.label :financial_institution %>
            <br/>
            <%= f.select :search_financial_institution, options_for_select(@search_financial_institutions.collect{ |f| [f.financial_institution, f.financial_institution] }, 1 ), {}, { id: 'search_financial_institution' } %>
            <%else%>
            <%= f.label :financial_institution %>
            <br/>              
            <%= f.select :search_financial_institution, options_for_select([['-Select-', '']]), {}, { id: 'search_financial_institution', class: 'input_form' } %>
            <%end %>
          </div>  
        </div>

      </div>
    </div>
  </div>
<%end%>


<!-- Javascript code to handle events for dropdowns in the above form-->

<script type='text/javascript'>
  /* When user changes value of "Entity" dropdown, do the needful to update
   * the "Financial Institutions" dropdown
   */
   $(document).on("change", "#search_entity", function(event){
    $.ajax('update_search_financial_institutions', {
      type: 'GET',
      dataType: 'script',
      data: {
        search_entity: $("#search_entity").val()
      },
      error: function(jqXHR, textStatus, errorThrown) {
        return console.log("AJAX Error: " + textStatus);
      }
    });
  });
</script>

正如您在上面看到的,我们有 2 个下拉菜单 - 当第一个 (Entity) 下拉菜单的值发生变化时,第二个 (Financial Institution) 下拉菜单的值应该根据选择进行修改第一个下拉列表

这是我的 routes.rb 条目:

routes.rb

  get "/update_search_financial_institutions" => "search#update_financial_institutions", :as => :update_search_financial_institutions

这是我的控制器函数,当第一个下拉列表的值发生变化时将被调用:

search_controller.rb

  def update_financial_institutions
    logger.info "SearchController update_search_financial_institutions"

    @search_financial_institutions = Account.where(:entity => params[:search_entity])
    render :partial => 'search/update_financial_institutions.js.erb'
  end  

这些是我的相关部分:

search/update_financial_institutions.js.erb

$("#search_financial_institution").empty().append("<%=escape_javascript(render(:partial => 'search/empty')) %>").append("<%=escape_javascript(render(:partial => 'search/financial_institution', :locals => {:institutions => @search_financial_institutions} )) %>");

search/_financial_institution.html.erb

<option><%=institutions.financial_institution%></option>

执行上面显示的最后一部分时出现此错误:

错误

ActionView::Template::Error (undefined method `financial_institution' for #<Account::ActiveRecord_Relation:0x00007f65fb49a1c0>):
    1: <option><%=institutions.financial_institution%></option>

app/views/search/_financial_institution.html.erb:1:in `_app_views_search__financial_institution_html_erb___881295623429985414_70037991679400'
app/views/search/_update_financial_institutions.js.erb:1:in `_app_views_search__update_financial_institutions_js_erb___1405969071861178528_70037991897300'
app/controllers/search_controller.rb:15:in `update_financial_institutions'

但是当我检查 search_controller 中的 @search_financial_institutions 时,我发现名为 financial_institution 的列非常多。

那我为什么会收到这个错误?

您的 @search_financial_institutions 变量是一个 Active Record 关系,一个 collection 记录。您正在尝试在 collection 上调用 financial_institution,而 collection 不响应该方法,collection 中的各个记录会响应。

如果您想显示 collection 中的每条记录,请尝试将 search/_financial_institution.html.erb 文件更改为:

<% institutions.each do |institution| %>
  <option><%=institution.financial_institution%></option>
<% end %>