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")'}
    
    评论

报告相同问题?