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

Popular posts from this blog

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -