开发者

Clean URLs Using forward slash '/' in to_param with Rails (3)

Is this possible?

def to_param
  "#{id}%2F#{slug}"
end

This开发者_JAVA技巧 works in Chrome and Safari, but if Firefox you see the "%2F" in the address bar. Is there a cleaner way?


This is indeed an old post, but I want to build a bit on it.

If you don't want to have to handle an slug variable in your params, you really need to define that method to_param in your model

def to_param
  "#{id}/#{title}"
end

and set a route like so:

resources :posts, :id => /[0-9]+\/.+/

That way your link definition looks quite like a normal one:

link_to post.title, post_url(post)

Very simple: http://www.miguelsanmiguel.com/2011/03/17/slug-that-slash


Ok I tried this just now you won't need this:

def to_param
  "#{id}/#{slug}"
end

In routes.rb (replace what you want with what you need)

match "/expenses/:id/:slug" => "expenses#show", :as => :expense

Your link_to should look like this now

= link_to "something", expense_url(:id => expense.id, :slug => expense.slug)

hope this helps a bit


Have a look at Friendly ID -- it will eliminate the ID entirely and just use the slug. It is Rails 3 compatible as well.


maybe something like this can help you

match "/foo/:id", :to => redirect("/bar/%{id}s")

check out the section The "Redirect Method" in this article about rails3 and routes

http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜