유사한 게시물에서 일반적으로 이전 줄에서 일부 닫는 문자가 누락 된 경우라는 것을 알지만 여기에서는 볼 수 없습니다. Courses 리소스는 Schools 내에 중첩되어 있으며 user_id와 school.id를 모두 인쇄 할 수 있습니다.
오류는 여러 조건 (user_id : current_user.id AND school_id : @ school.id)이있는 Course.where 절에있는 것 같습니다. 위의 단일 조건 where 절이 제대로 작동하는 것 같습니다.
course_controller.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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다