url_view/controller 이외에 for (@hogeable) 를 사용할 때
4555 단어 Rails
전제로 삼다
4
4
Routing
잠깐만요.그런 거 싫어[1] pry(main)> Rails.application.routes.url_helpers.url_for(like.likeable)
ActionController::UrlGenerationError: No route matches ・・・(以下略
😨url_for의 정의
Url Helper 앞에서 Routing UrlFor 같은 모듈을 읽고 있는데 거기서
actionview/lib/action_view/routing_url_for.rb에서 발췌하다
method = _generate_paths_by_default ? :path : :url
builder = ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.send(method)
case options
when Symbol
builder.handle_string_call(self, options)
when Class
builder.handle_class_call(self, options)
else
builder.handle_model_call(self, options)
end
이렇게 정의된HelperMethod Builder를 사용해 보세요.
3이놈안에서 많이 노력했어, 응.(제대로 읽지 못했다).
그러므로
def path_for(hogeable)
builder = ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.send(:path)
builder.handle_model_call(Rails.application.routes.url_helpers, hogeable)
end
def url_for(hogeable)
builder = ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.send(:url)
builder.handle_model_call(Rails.application.routes.url_helpers, hogeable)
end
이렇게 정의를 내리면...[1] pry(main)> path_for(like.likeable)
=> "/blogs/1"
キタ——————(゜∀゜)——————!!라는 뜻이다.
최후
확실하다for라고 할 수 있어요.
근데 아마 원래는 이렇게 안 써도 될 것 같은데, 이게 라일스의 생각이겠죠
Reference
이 문제에 관하여(url_view/controller 이외에 for (@hogeable) 를 사용할 때), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/YusukeIwaki/items/2119ad0882040b9698eb텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)