weixin_33713350 2013-02-04 05:54 采纳率: 0%
浏览 27

js.erb中的options_for_select

I have this in file dvbd.js.erb and it works fine. The expected behavior is that the Dave div will have the set of name and id listed inside of it.

<%@dimvers = DimensionVersion.select("name, id").where(:dimension_id => params[:id]).all %>


$("#dave").html("<%= @dimvers.collect { |d| [d.name, d.id]} %>");

This is in applications.js, and the .post calls dvbd.js.erb fine:

jQuery.ajaxSetup({
  'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript") }
});

$.fn.subSelectWithAjax = function() {
  var that = this;

  this.change(function() {
    $.post("/dimensions/dvbd", {id: that.val()}, null, "script");
  });
}

$(document).ready(function(){
$("#dimver_dimension_id").subSelectWithAjax();
});

However, when I change the code in dvbd.js.erb to the following, it does not work. I expect it to change the options for select box #dimension_id. But instead it only changes the select box to have no contents/options if @dimvers happens to return no matching records. Then #dimension_id is no longer responsive.

<%@dimvers = DimensionVersion.select("name, id").where(:dimension_id => params[:id]).all %>

$("#dimension_id").html("<%= options_for_select(@dimvers.collect {|d| [d.name, d.id] }).gsub(/n/, '') %>");

Here is the code in show.html.erb:

<div id = 'hierarchypanel'>
    <%= collection_select(:dimver, :dimension_id, Dimension.all, :id, :title ) %>
    <%= collection_select(:dimension,:id, @dimension_versions,:id, :name)  %>
</div>
  • 写回答

2条回答 默认 最新

  • weixin_33739646 2013-02-04 06:05
    关注

    best way is to have a partial and have the select_tag in the partial, then call that partial from your .js.erb

    Ex:

    #_select_partial.erb
    <%= options_for_select(@dimvers.collect {|d| [d.name, d.id] }).gsub(/n/, '') %>
    
    #dvbd.js.erb
    $("#dave").html("<%= raw escape_javascript(render(:partial => 'select_partial')) %>")
    
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误