리소스를 상상해보십시오 : /users/14/notifications
. GET, GET/edit, POST, DELETE와 같은 몇 가지 HTTP 동사/메소드를 구현합니다.RoR의 모든 HTTP 동사/메소드 작업에서 컨트롤러 작업의 일부를 재사용 할 수 있습니까?
모든 네 가지 동작은 모든 알림을 검색하고, 모든 알림을 검색하고, 쉽게 액세스 할 수 있도록 해시를 만들고, 다른 사용자로부터 특정 사용자 액세스를 가져 와서 특별한 작업을 수행하는 등 일부 작업을 공유합니다.
로직을 유지하기 위해 어떻게이 7 loc을 재사용 할 수 있습니까? 레일즈의 모든 동사에 대해 들었지만 사용 방법을 모른다. 또한 4 가지 실제 동작에서 그 결과 (몇 개의 행)를 사용하는 방법을 알지 못합니다.
당신이 눈치 챘을 수도로def any
@notifications = Notification.find_by etc...
// do something here to create
@reverse_notifications_hash = ...
// and something else
@super_special_access = ...
end
def show
// Only specific logic here
// Render using @notifications
end
def edit
// Only specific logic here
// Render form using @notifications,
// @reverse_notifications_hash and
// @super_special_access
end
def update
// Only specific logic here
// Fetch something else special to not override stuff or be extra efficient
more_special = ...
// Do update stuff with @notifications, @super_special_access and more_special
end
, 나는 프로 루비/Railser 아니에요 :
내가 좋아하는 뭔가를 기대하는 것이다. (구문이 잘못되었을 수도 있습니다.) 나는 매우 궁금합니다.
RoR에서 실제로 어떻게 작동합니까?
[필터] (http://guides.rubyonrails.org/action_controller_overview.html#filters)가 명확한 방법입니다. – alony