在Rails中调用更新函数后,渲染不起作用

时间:2014-07-19 02:06:19

标签: ruby-on-rails

我的控制器的更新功能如下所示:

def update
    @cohort = Cohort.find(params[:cohort][:id])
    @cohort.update_attributes(params[:cohort])

    respond_to do |format|
        format.js {render action: "show", layout: "courses"}
    end
end

当我触发update函数(通过js)时,在我的Rails控制台中获取以下内容:

Started PUT "/cohorts/41" for 127.0.0.1 at 2014-07-18 21:57:26 -0400
Processing by CohortsController#update as */*
  Parameters: {"cohort"=>{"id"=>"41", "user_id"=>"17", "room_id"=>"16"}, "id"=>"41"}
  Cohort Load (0.1ms)  SELECT "cohorts".* FROM "cohorts" WHERE "cohorts"."id" = ? LIMIT 1  [["id", "41"]]
   (0.0ms)  begin transaction
   (0.3ms)  UPDATE "cohorts" SET "room_id" = 16, "updated_at" = '2014-07-18 21:57:26.303928' WHERE "cohorts"."id" = 41
   (8.5ms)  commit transaction
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 17 LIMIT 1
  Course Load (0.1ms)  SELECT "courses".* FROM "courses" WHERE "courses"."id" = 21 LIMIT 1
  Timeperiod Load (0.1ms)  SELECT "timeperiods".* FROM "timeperiods" WHERE "timeperiods"."id" = 37 LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 17 LIMIT 1
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE (role = 'faculty' OR role = 'admin')
  FacultyReport Load (0.1ms)  SELECT "faculty_reports".* FROM "faculty_reports" WHERE "faculty_reports"."cohort_id" = 41 LIMIT 1
  Room Load (0.1ms)  SELECT "rooms".* FROM "rooms" 
  Room Load (0.1ms)  SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = 16 LIMIT 1
  Enrollment Load (0.1ms)  SELECT "enrollments".* FROM "enrollments" WHERE "enrollments"."cohort_id" = 41
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 17 LIMIT 1
  Rendered cohorts/show.html.erb within layouts/courses (12.3ms)
  Rendered layouts/_header.html.erb (2.1ms)
  Rendered layouts/_leftnav.html.erb (0.8ms)
  Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 44.7ms (Views: 31.1ms | ActiveRecord: 10.0ms)

请注意Rendered cohorts/show.html.erb within layouts/courses (12.3ms)。在浏览器本身中,不会进行刷新或重新渲染。如果我手动刷新页面,我可以看到已经进行了更改,但页面本身也发生了变化。

此外,如果我将render更改为redirect_to,我会(在控制台中)获得一个大约14次迭代的重定向循环,然后停止并且浏览器中没有任何更改。

1 个答案:

答案 0 :(得分:0)

respond_to

从您的评论&看看你的代码,你需要记住Rails repond_to阻止如何工作

当您致电format.js时,Rails会自动在[action_name].js.erb文件夹中查找views/controller。当你从ajax / JS点击update动作时,它将基本上运行,它将执行函数&然后寻找update.js.erb来运行。

您的代码有几个问题:

#app/controllers/your_controller.rb
Class YourController < ApplicationController
    def update
        @cohort = Cohort.find(params[:cohort][:id])
        @cohort.update_attributes(params[:cohort])

        respond_to do |format|
            format.js {render layout: "courses"} #-> automatically loads views/your_controller/update.js.erb
        end
    end
end

-

<强>更新

您应该考虑的其他事项是update功能 - 特别是您正在尝试使用裸params更新模型(您应该使用strong_params):

def update
    @cohort = Cohort.find params[:cohort][:id]
    @cohort.update(cohort_params)
end  

private

def cohort_params
    params.require(:cohort).permit(:your, :params)
end

编辑:

我将format.js {render action: "show", layout: "courses"}更改为format.js {render "update.js.erb"},然后按预期工作。