kenshin54
kenshin54

Reputation: 1

link_to and no route match problem

I have try to use link_to like so:

<%= link_to post.title, {:controller => 'posts', :action => 'show', :id => post.title}, :title => post.title %>

If title contains a period (.), I get the following error:

No route matches {:controller=>"posts", :action=>"show", :id=>"test.title"}

But if no period is included, everything works.

Can anyone help me?

I found the solution.http://www.ruby-forum.com/topic/101911#222985

Upvotes: 0

Views: 2007

Answers (3)

dombesz
dombesz

Reputation: 7899

The solution that you linked is the worst one. Overriding rails internals to fit your problem is wrong. I rather use the gem called friendly_id, this will creates a url proof slug from your id. It's easy to set up and use.

https://github.com/norman/friendly_id

Upvotes: 1

sren
sren

Reputation: 3623

It thinks that in the route it is expecting the format ".title" and that has not been configured. Because your route would look something like this "/posts/test.title" could you not rather use "/posts/test-title" as that is a better url structure for most web servers?

If you do need to hack it, in rails 2 you can use the ":requirements" option like this:

map.connect '/post/:id/:title', :controller => 'forum', :action => 'show_post', :requirements => { :title => /.*/ }

Upvotes: 0

Dty
Dty

Reputation: 12273

The problem is :title => post.title Try using the post object in there instead.

link_to post.title, :controller => 'posts', :action => 'show', :id => post

or even better

link_to post.title, post

Upvotes: 1

Related Questions