How do I override the controller name for a rails url helper? -
i have post
model category
attribute , corresponding postscontroller
.
i override default url helper produce folowing:
@post = post.create(category: 'posts') <%= link_to 'post link', @post %> # /posts/1 @post = post.create(category: 'articles') <%= link_to 'article link', @post %> # /articles/2
i call default url helper post
have create different urls based upon category
column.
update
i ended overriding url helpers:
module postshelper def post_path(post, options={}) self.send("#{post.category}_path", post, options) end def post_url(post, options={}) self.send("#{post.category}_url", post, options) end end
named routes want:
get 'posts/:id', to: 'posts#<controller-action>', as: 'posts'
<%= link_to 'post link', posts_path(@post) %>
get 'articles/:id', to: 'posts#<controller-action>', as: 'articles'
<%= link_to 'article link', articles_path(@post) %>
see here: http://guides.rubyonrails.org/routing.html#naming-routes
Comments
Post a Comment