我从类似的帖子中看到,这通常是前一行缺少一些结束字符的情况,但我在这里没有看到。课程资源嵌套在学校中,我可以打印出user_id和school.id。
错误出现在Course.where子句中,该子句包含多个条件(user_id:current_user.id AND school_id:@school.id)上面的一个条件似乎很好。
课程_控制器.rb
class CoursesController < ApplicationController
before_action :set_school
helper_method :calculated_grade_stub
# GET /courses
# GET /courses.json
def index
if @school.nil?
@courses = Course.where(user_id: current_user.id)
else
@courses = Course.where(user_id: current_user.id AND school_id: @school.id)
end
end
运算符是&&
或and
,而不是AND
。
然而,这并不是问题的根本原因:您似乎对Hash
文字的一般语法感到困惑。Hash
文本中的条目用逗号分隔,而不是用运算符分隔:
@courses = Course.where(user_id: current_user.id, school_id: @school.id)
还要注意,if
是一个表达式,因此会返回一个值,因此可以很容易地将该方法重构为
def index
@courses = if @school.nil?
Course.where(user_id: current_user.id)
else
Course.where(user_id: current_user.id, school_id: @school.id)
end
end
你可以试试这个:
@courses = Course.where('user_id = ? AND school_id = ?',current_user.id , @school.id)