元素更新时RJS错误“对象不支持此属性”

时间:2011-10-19 06:30:20

标签: ruby-on-rails ajax jquery rjs

我在控制器中有以下代码。 我根据另一个下拉框中的选择动态填充下拉框。

def  update_releases 
  project = Project.find(params[:project_id]) 
  releases = project.releases
  puts "releases==#{releases}" 
  render :update do |page| 
  page.replace_html 'releases', :partial => 'releases', :object => releases 
end 

查看代码:

-form_tag reports_path(report_type=1),:method => :get, :multipart => true ,:id => "filter" do  
%table.grid.full
%tr      
  %td.grid.full_panels{:style => "width: 20%"}
    Project:
  %td.grid.full_panels{:style => "width: 20%"}
    //= select_tag "projects",options_from_collection_for_select(projects,"id","name",params[:projects]),{:onchange => "#{remote_function(:url  => {:action => "update_releases"},:with => "'project_id='+value")}"} 
    = select_tag "projects",options_from_collection_for_select(projects,"id","name",params[:projects]), :class => "update_releases"


  %td.grid.full_panels{:style => "width: 20%"}
    Releases:
  %td.grid.full_panels{:style => "width: 20%"}
    <div id="releases">
    = render :partial => 'releases', :object => @releases
  %td.grid.full_panels{:style => "width: 20%"}
    Cycles:
  %td.grid.full_panels{:style => "width: 20%"}
    <div id="cycles">
    = render :partial => 'cycles', :object => @cycles

%tr      
  %td.grid.full_panels{:style => "width: 20%"}
  %td.grid.full_panels{:style => "width: 20%"}
  %td.grid.full_panels{:style => "width: 20%"}
    =submit_tag "Submit"

= javascript_include_tag "pages/ic"

部分代码:      = select_tag“releases”,options_from_collection_for_select(releases,“id”,“name”,params [:releases])

Jquery的:       //根据项目下拉选项更改版本下拉列表。

$('.update_releases').live("change", function(){
  $.ajaxSetup({beforeSend: function(xhr) {xhr.setRequestHeader("X-CSRF-Token", $("meta[name='csrf-token']").attr("content")); }});
  var project_id=($(this).val());
  $.post('/reports/update_releases', {project_id: project_id});
 return false;
})

我收到一个错误元素不支持此属性“在弹出菜单中选择下拉框中的选项。请在这里帮助我。

1 个答案:

答案 0 :(得分:1)

简单的解决方法是尝试在控制器中使用此代码:

respond_to do |format| 
  format.js render :partial => 'releases', :locals { :releases => releases }
end

releases.js.haml

   $('#releases').html('#{render :partial => '<some_old_html_partial>', :locals => {:releases => releases}}')