简体   繁体   中英

Ajax call with dropdown list

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!

确保您的Ajax调用请求JS格式:

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

If you want to make an ajax call you need to include:

"data-remote" => true

See here for more information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM