weixin_33743248 2015-05-18 11:46 采纳率: 0%
浏览 29

带下拉列表的Ajax调用

I have trying to follow this question but I am stuck. In my controller I have:

 def index
    if params[:sort] == 'stars'
      @projects = [Project.first]
    else
      @projects = Project.all
    end

    respond_to do |format|
      format.html
      format.js { render 'populate_projects', :formats => [:js] }
    end
  end

in routes:

get '/inspire/:sort' => 'projects#index'

in view:

   = collection_select :project, :id, Project.all, :id, :name, {}, {:onchange => '$.get("/inspire/stars")'}
   %div#normal
    = render 'projects_list'
   %div#stars{ style: 'display: none' }

my _projects_list.html.haml has:

  %div
    - @projects.each do |project|
      %div
        %p
         #more code...

and finally in populate_projects.js.haml:

:plain
  $("#stars").html("#{escape_javascript render(partial: 'projects/projects_list')}");
  $("#normal").hide();
  $("#stars").show();

Probably the program doesn't make sense as I am testing if ajax call is working. However, what should happen is when I change the state of dropdown an ajax call must be made which renders 'propulate.js.haml' and list of projects must change from all to just first, but is not. In my terminal I can see that call is being made but 'populate.js.haml' is never rendered. Can someone please help!

  • 写回答

2条回答 默认 最新

  • weixin_33744854 2015-05-18 12:19
    关注

    Make sure your Ajax call requests the JS format:

    = collection_select :project, :id, Project.all, :id, :name, {}, {:onchange => '$.get("/inspire/stars.js")'}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line