많은 before_actions가있는 컨트롤러가있는 앱에서 작업하고 있습니다. 대부분은 설정 한 인스턴스 변수에 의해 서로 연결되어 있습니다. 예를 들어 :여러 before_action 호출이 잘못된 코드 스타일입니까?
def first_action
@first_variable = Something.new
end
def second_action
if @first_variable
@second_variable = Other.new
end
end
컨트롤러는 다음과 같습니다
이class ExampleController < ApplicationController
before_action :first_action, only: [:index, :show, :create]
before_action :second_action, only: [:index, :show, :create]
before_action :third_action, only: [:index, :show, :create]
before_action :fourth_action, only: [:index, :show, :create]
before_action :fifth_action, only: [:index, :show, :create]
before_action :sixth_action, only: [:index, :show, :create]
before_action :seventh_action, only: [:index, :show, :create]
def index
# some code
end
def show
# some code
end
def create
# some code
end
private
# all of the before_action methods
end
그것은보기 내 관점에서 이해하는 것이 정말 어렵다. 각각의 메소드에는 많은 코드가 있습니다. 또한이 컨트롤러를 상속하는 컨트롤러가 있으며 이러한 컨트롤러의 일부 또는 전부를 사용합니다.
class ExampleController < ApplicationController
def index
first_action
second_action
third_action
fourth_action
fifth_action
sixth_action
seventh_action
# some code
end
def show
first_action
second_action
third_action
fourth_action
fifth_action
sixth_action
seventh_action
# some code
end
def create
first_action
second_action
third_action
fourth_action
fifth_action
sixth_action
seventh_action
# some code
end
private
# all of the before_action methods
end
훨씬 더 보이지 않는 :
나는 그것을로드 각 방법의 변수하지만 이것에 대해 명시하는 것이 좋습니다 들었어요. 가독성을 높이기 위해 리팩터링하는 방법이 있습니까? 아니면 현재 솔루션을 고수해야합니까?
여러 개의 'before_actions'을 갖는 데는 아무런 문제가 없습니다. 그러나 한 번의 조치로 수집 될 수있는 경우가있는 것처럼 보입니다. – Matt
나는 당신의 생각 @ 매트 고마워요 그리고 만약 당신이 내 문제에 대한 해결책으로 그것을 확인할 수있는 대답으로 추가하면 :) – zeth
완료, 도움이 듣고 다행! – Matt