[edge Rails] Inflector.parameterize for easy slug generation
David has just commited an parameterize method for easy slug generation. This means it strips out all special characters so that it can be savely used in URLs. It replaces anything but a-z and 0-9 with a “-” (you can pass a custom seperator).
Example to generate nice URLs for your models:
def to_param "#{id}-#{name.parameterize}" end
"$%hello I'm a sentence with & a löt òf SPECIAL chars+".parameterize #=> -hello-i-m-a-sentence-with-a-lt-f-special-chars-
My find_by_param plugin, that helps you a lot with working with nice URLs, uses a custom encoding method to do this. Perhaps I will change that some time soon. ;)
Update:
The comments on the commit pointed to two really nice projects:
1. stringex – which is a bit of a overkill. *tries to solve everything* but creates really aweseome slugs by translating special chars ($ to dollar, etc.)
2. slugalizer – a ruby slugalizer which ses ActiveSupport for platform-consistent normalization. It also does nice formatting like: Åh, räksmörgåsar! => ah-raksmorgasar”
Update2
This feature was extended earlier today. Not nice conversions like Malmö = malmo or Garçons = garcons are supported.
[edge Rails] shallow nesting of routes
Ok, finally. This is the first post of a series about some new exciting edge rails features. ;)
Here at Railslove most of our projects are living on the edge (thanks to braid!) ;) and we try to keep close track of what’s happening in the rails master branch.
Today I’ve found a commit that is actually a few days old but this will clean up a lot of my nested routes.
Imagine your application has a User who has_many :posts which again has_many :comments. Your routes would look something like:
map.resources :users do |user| user.resources :posts do |post| post.resources :comments end end
This defines the following helpers and URLs
users_url #=> /users/ user_posts_url #=> /users/1/posts/ user_post_comments_url #=> /users/1/posts/10/comments
however only the full nested routes are available.
/posts/10 or /comments/10 are not available and you need to declare those seperately:
map.resources :posts do |post| post.resources :comments end map.resources :comments
This commit now allows you to add a :shallow => true option which does this automatically for you. This is great and shortens the routes.rb a lot.
The example above would then just look like:
map.resources :users, :shallow => true do |user| user.resources :posts, :shallow => true do |post| post.resources :comments end end
and post_url, comment_url, post_comments_url,… get also defined.
very nice!
Have a look at the commit message and source code for more information.
Update:
Georg of SalesKing.eu fame pointed me to Ryan’s great post about the :shallow option.
zwei Ruby/Rails Bücher zum kostenlosen Download
O’Reilly verschenkt anlässlich der Railconf EU zwei Ruby/Rails Bücher. Das erinnert mich doch irgendwie an unsere Railscrowd Race #1 im April, als wir auch genau eines der Bücher verschenkt haben. ;)
Bei den Büchern handelt es sich um das Einsteigerbuch Praxiswissen Ruby on Rails und das Rails Kochbuch - beide auf deutsch.
Hier geht’s zum Download: http://www.oreilly.de/artikel/2008/08/rubyonrails.html