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 h开发者_如何学Goelp me?
I found the solution.http://www.ruby-forum.com/topic/101911#222985
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
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
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 => /.*/ }
精彩评论