이 SO에 내 첫 번째 질문입니다 :)
나는 RoR에 아주 새로운 그리고 난 당신이 여기 https://github.com/PingoUPB/PINGOWebApp을 찾을 수 PINGOWebApp을 이해하려고합니다. 그들은 app/services/generic_question.rb에서 상속 한 app/services/(예 : number_question.rb, text_question.rb)의 다양한 유형의 질문에 "question"모델 (app/models/question.rb)을 지정했습니다.의 RoR : 나가서 설명하자면 NameError : 초기화되지 않은 상수 (서비스)
class GenericQuestion < Delegator
def initialize(question)
super
@question = question
end
def __getobj__ # required
@question
end
def __setobj__(obj)
@question = obj # change delegation object
end
def to_model
@question.to_model
end
def has_settings?
false
end
def add_setting(key, value)
@question.settings ||= {}
@question.settings[key.to_s] = value.to_s
end
def self.model_name
Question.model_name
end
def self.reflect_on_association arg
Question.reflect_on_association arg
end
alias_method :question, :__getobj__ # reader for survey
end
여기 내 첫 번째 질문에 온다 : 응용 프로그램/서비스
에는 서비스 발전기가 없기 때문에 1), 자신이 만든해야하는 모든 루비 파일을/손으로, 그들은하지 않았습니다? 아니면 다른 방법이 있습니까?
2) I 프로젝트를 포크와 dragdrop_question.rb라는 손으로 다른 서비스를 추가하고 question_controller.rb로 통합 :
class QuestionsController < ApplicationController
...
def new
@question_single = SingleChoiceQuestion.new.tap { |q| q.question_options.build }
@question_multi = MultipleChoiceQuestion.new.tap { |q| q.question_options.build }
@question_text = TextQuestion.new
@question_number = NumberQuestion.new #refactor this maybe?
@question_dragdrop = DragDropQuestion.new.tap { |q| q.answer_pairs.build }
end
...
end
는 또한 뷰 적응 로컬 테스트./questions/new에 NameError가 있습니다. 초기화되지 않은 상수 QuestionsController :: DragDropQuestion.
나는 오류가 사라진 question_controller.rb에
require_dependency "app/services/dragdrop_question.rb"
을 추가 할 수 있지만 그들은 그것을 그런 식으로 아무 짓도하지 않은 경우. 그러면 컨트롤러에 서비스를 어떻게 소개할까요?
미리 도움을 주셔서 감사합니다. 특히 튜토리얼이나 책 참고서에 도움을 주셔서 감사합니다. controller-model-view-service 스키마를 설명합니다.
이렇게하면 내 문제가 해결되었습니다. 하나의 파일에 여러 클래스를 정의하고 있다는 것을 깨달았습니다. 여분의 클래스를 자신의 파일로 옮기면 해결됩니다. – taylorthurlow