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

Is it possible?

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

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

+4
source share
4 answers

This is a really old post, but I want to work on it a bit.

If you do not want to handle the slug variable in your parameters, you really need to define this to_param method in your model

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

and set the route as follows:

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

So your link definition looks like normal:

  link_to post.title, post_url (post) 

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

+10
source

Ok, I tried it now, you won’t need it:

 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 now look like this:

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

hope this helps a little

+3
source

Look at Friendly ID - it will completely eliminate the identifier and just use slug. It is also compatible with Rails 3.

+2
source

maybe something like this can help you

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

check out the Redirect Method section in this article on rails3 and routes

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

0
source

All Articles