js文件不会更改视图

时间:2015-05-18 17:09:13

标签: javascript jquery ruby-on-rails ajax

我跟随我的select标签触发ajax调用:

  = select('project', 'sort', sort_type, {prompt: 'Select method'})
  #sorted{ style: 'display: none' }
  #normal
    = render 'projects_list'

我的javascript,其中#project_sort是select tag的id:

$("document").ready(function() {
  $("#project_sort").change(function() {
    var state = $('select#project_sort :selected').val();
    jQuery.getJSON('/inspire/' + state+ '.js');
  });
})

我的路线:

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

在我的控制器中:

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

    respond_to do |format|
      format.html
      format.js { render 'populate_projects' }
    end
  end

populate_projects.js.haml文件:

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

和_projects_list.html.haml:

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

应该发生的事情是当select标签的状态发生变化时,应该进行ajax调用,并且视图必须从所有项目更改为仅第一个,但事实并非如此。在我的终端中,我可以看到在状态更改时进行调用,只选择了第一个项目并且渲染了populate_project.js.haml,但是我看不到我的broswer的任何变化。

编辑 - 控制台:

Started GET "/inspire/stars.js" for 127.0.0.1 at 2015-05-18 22:18:16 +0530
Processing by ProjectsController#index as JS
  Parameters: {"sort"=>"stars"}
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  Project Load (0.3ms)  SELECT  "projects".* FROM "projects"  WHERE "projects"."deleted_at" IS NULL  ORDER BY "projects"."id" ASC LIMIT 1
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered projects/_forked_from.html.haml (0.2ms)
  Rate Load (0.2ms)  SELECT "rates".* FROM "rates"  WHERE "rates"."rateable_id" = ? AND "rates"."rateable_type" = ? AND "rates"."dimension" = 'stars' AND "rates"."rater_id" = 1  [["rateable_id", 1], ["rateable_type", "Project"]]
  RatingCache Load (0.1ms)  SELECT  "rating_caches".* FROM "rating_caches"  WHERE "rating_caches"."cacheable_id" = ? AND "rating_caches"."cacheable_type" = ? AND "rating_caches"."dimension" = 'stars' LIMIT 1  [["cacheable_id", 1], ["cacheable_type", "Project"]]
  Rendered projects/_projects_list.html.haml (22.9ms)
  Rendered projects/populate_projects.js.haml (24.5ms)
Completed 200 OK in 37ms (Views: 29.0ms | ActiveRecord: 1.3ms)

0 个答案:

没有答案