ApplicationController
에 작성하고 helper_method로 정의하십시오.
class ApplicationController < ActionController::Base
helper_method :current_time
def current_time
Time.now
end
end
예를
를 들어 지금 당신은 컨트롤러 나 뷰에 사방
current_time
살았어 수 있습니다.
또한 모듈/클래스를 별도로 작성하고 헬퍼 메소드를 정의 할 수 있습니다. 테마
을 변경 한 후에 당신이 당신의
ApplicationController
뿐만 아니라
UPD에이 파일을 포함해야보다 나는 실제 질문에 대해 생각하지 않았다. 하지만 당신의 접근 방식이 최고로 고개를 끄덕 였다고 말할 수 있습니다.
테스트하기 어려운 새 기능을 만드는 대신 리소스을 새로 만들어야합니다. 그래서 새로운 리소스 (컨트롤러)를 생성하십시오 : 버전 및이 컨트롤러 주위에 놀아 라.
예를 들어 그것이 작동하는 방법 :
/versions/pages/132
/versions/comments/1003
그것을 실현하는 방법 :
match "/versions/:model/:id", :to => "versions#index"
를 컨트롤러에서 : 물론
class VersionsController < ActionController::Base
def index
@object = my_type.find(params[:id])
@versions = @object.versions
end
private
def my_type
params[:model].constantize
end
end
을 당신에게 경로를 방식을 변경할 수 있습니다 원하는 :
match "/:model/:id/versions", :to => "versions#show"
이제 /pages/testpage/versions
은 새로운 이상한 논리가없이 잘 작동합니다.
UPD 2
은이 경로를 가지고 상상 :
match "/:model/:id/versions", :to => "versions#index", :as => :versions
을 그리고 이것은 객체 :
<%= link_to "Versions of this page", versions_path(:model => @page.class.to_s, :id => @page.id) %>
<%= link_to "Versions of this hotel", versions_path(:model => @hotel.class.to_s, :id => @hotel.id) %>
<%= link_to "Versions of this comment", versions_path(:model => @comment.class.to_s, :id => @comment.id) %>
:
@page = Page.last
@hotel = Hotel.find(123)
@comment = @page.comments.first
우리가 어떻게 버전에 대한 링크를 만듭니다
어쩌면 나는 다만 이해하지 않는다 그러나 이것은 나의 문제를 해결하는 것을 보이지 않는다. 나는 조금 더 질문을 바꿨다. 이제는 더 명확 해지기를 바란다. –
예. 이제는 또 다른 질문입니다 :) – fl00r
미안 해요. 설명을 마친 후 질문을 검토하는 것을 잊었습니다. –