Question

I'm trying to have URL rewriting with parameterize, as explain here : How do I rewrite URL's based on title?

Here is my model :

class Article < ActiveRecord::Base
  belongs_to :category

  self.per_page = 5

  def to_param
    "#{title.parameterize}"
  end
end

And my link :

<%= link_to(article.title, blog_article_path(article), {:class => "blog_title"}) %>

THe problem is that I don't have a link like /blog/article/"my-article-title" but I have /blog/article."my-article-title", which is wrong and not interpreted.

Do you know the reason ?

My route.rb :

get "blog/index"
get "blog/category"
get "blog/article" (I don't use the show action of my article controller, is it the reason ?)

resources :categories

resources :articles

Thanks

Was it helpful?

Solution

Using link_to the way you are by passing in a resource only really works when you actually use resources in your route file.

This is the difference (rake routes output) with using a non-resourceful route and one generated by resources:

blog_article GET    /blog/article(.:format)

article GET    /articles/:id(.:format)

When you use article_path(@article) it will fill in :id with the id of the resource.

I'd advise you to use the show action of the articles controller so you would have /blog/articles/:id or you could do something like this if you really want that routing:

get "blog/article/:id" => "articles#show", :as => 'blog_article'

which turns into:

blog_article GET    /blog/article/:id(.:format)

The official guides have some good info on this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top