ruby-on-rails - 예제 - start rails
lib 폴더 모듈에서 url_for 사용하기 (1)
나는 "type"이라는 필드와 "value"라는 필드를 가진 모델을 가지고있다. type 필드는 렌더링 전에 "값"이 파싱되는 방법을 결정합니다. 이것을 쉽게 확장 할 수 있기를 바라고, lib 폴더에 다양한 "포매터"클래스를 넣을 것입니다.
내 "포매터"중 한 명이 url_for를 호출합니다.
class CustomTypeFormatter
include ActionView::Helpers::TextHelper
include ActionView::Helpers
def show
raw sanitize( auto_link( value ) )
end
def get_url(page)
url_for( :controller => :my_controller, :action => :show, :path => page.path )
end
end
문제는 url_for가이 오류를 발생시키고 있다는 것입니다.
undefined local variable or method `_routes'
나는 내가 포함 된 것을 잃어버린 것 같아. 누구나해야 할 일이 무엇인지 알고 있습니까?
** 업데이트 * * *
다음은 스택 추적의 일부입니다.
actionpack (3.0.7) lib/action_dispatch/routing/url_for.rb:131:in `url_for'
actionpack (3.0.7) lib/action_view/helpers/url_helper.rb:99:in `url_for'
그래서 나는 그것이 ActiveDispatch 의존성이어야한다고 생각하지만, 나는 알 수 없다.
승리!
include ActionView::Helpers
include ActionDispatch::Routing
include Rails.application.routes.url_helpers