知识点:polymorphic(多态路由辅助方法)

主体写法:polymorphic_url polymorphic_path polymorphic_url(post) # => “http://example.com/posts/1” polymorphic_url([blog, post]) # => “http://example.com/blogs/1/posts/1” polymorphic_url([:admin, blog, post]) # => “http://example.com/admin/blogs/1/posts/1” polymorphic_url([user, :blog, post]) # => “http://example.com/users/1/blog/posts/1” new_polymorphic_path(Post) # “/posts/new” new_polymorphic_url(Post) # “http://example.com/posts/new” edit_polymorphic_path(post) # “/posts/1/edit” edit_polymorphic_url(post) # “http://example.com/posts/1/edit”

difference between _url and _path while using the routes in rails

*_path are for views because ahrefs are implicitly linked to the current URL. So it’d be a waste of bytes to repeat it over and over. In the controller, though, *_url is needed for redirect_to because the HTTP specification mandates that the Location: header in 3xx redirects is a complete URL.

_path helpers provide a site-root-relative path. You should probably use this most of the time.

_url helpers provide an absolute path, including protocol and server name. It mainly use these in emails when creating links to the app on the server. They should mainly be used when providing links for external use.

()[http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html] ()[http://apidock.com/rails/ActionController/PolymorphicRoutes/polymorphic_url] ()[http://apidock.com/rails/ActionController/Base/url_for]