views:

57

answers:

2

I have a class Post

I want the default URL of each posts to be http://domain.com/9383 instead of http://domain.com/posts/9383

I tried to fix it in the routes. I manage to accept domain.com/222 but if I use <%= url_for(@posts) %> I still get domain.com/posts/222

How can I do it? Thank you

A: 

If you're using url_for, there's no way to tell it to omit the /posts/ section. I think you would need to create helper, maybe in application_helper.rb

def post_url(post)
  "/#{post.id}"
end
Sikachu
+5  A: 

You can't change the behaviour of url_for(@post) with routes. url_for will assume a map.resources setup if an ActiveRecord instance is passed to it.

You should rather do this:

# routes.rb
map.post ":id", :controller => "posts", :action => "show"

# named route
post_path(@post)

# full link_to
link_to @post.title, post_path(@post)
August Lilleaas