1
두 가지 방법의 차이점은 무엇입니까 궁금합니다. ActionView::Helpers::UrlHelper.url_for
및 ActionController::UrlWriter.url_for
?url_for 메소드의 차이점은 무엇입니까?
두 가지 방법의 차이점은 무엇입니까 궁금합니다. ActionView::Helpers::UrlHelper.url_for
및 ActionController::UrlWriter.url_for
?url_for 메소드의 차이점은 무엇입니까?
UrlHelper
의 버전은 내보기에서 사용할 수있는 버전입니다. 이 url_for
에 전달 된 파라미터는 턴에 위임 ActionController::UrlWriter.url_for
에 controller.url_for
에 해시 위임되면 경우
def url_for(options = {})
options ||= {}
url = case options
when String
escape = true
options
when Hash
options = { :only_path => options[:host].nil? }.update(options.symbolize_keys)
escape = options.key?(:escape) ? options.delete(:escape) : true
@controller.send(:url_for, options)
when :back
escape = false
@controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
else
escape = false
polymorphic_path(options)
end
escape ? escape_once(url) : url
end
: 여기 소스이다.
ActionView::Helpers::UrlHelper.url_for
의 버전은 다른 유형의 인수와 함께 호출 될 때 추가 동작을 추가합니다. 문자열이 전달 된 경우 이스케이프 처리됩니다. symbol : back을 사용하여 호출 할 때 사용할 수있는 경우 referer에 연결하거나 JavaScript history.back()
호출로 돌아갑니다.
그래서 url_for 뷰는 ActionController :: UrlWriter.url_for의 래퍼 일 뿐이므로? – Eimantas
당신이 해시로 전화 할 경우, 그렇습니다. 그것은 답안에서 다루었 듯이 약간의 여분의 행동을 제공합니다. – mikej