Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.
no thanks
So I’m learning the latest Rails release by converting an existing app to it. This app used namespaced controllers for its admin section.
New routes features have really cleaned this up.
Before
1 2 3 4 5 |
map.resources :users, :controller => 'admin/users', :name_prefix => 'admin_', :path_prefix => 'admin' |
After
1 2 3 4 |
map.namespace(:admin) do |admin| admin.resources :users end |
Much cleaner.
Then I began to take advantage of Rails 2.0’s smarter #form_for courtesy of simple_helpful in the non-admin portion of the app.
1 2 3 |
<% form_for @user do |form| %> <% end %> |
If that user object is a #new_record? that #form_for is going to generate the following html:
<form action="/users" method="post" class="new_user" id="new_user">
</form>
If that user object is not a #new_record? that #form_for is going to generate the following html (assuming its id is 1):
<form action="/users/1" method="post" class="edit_user" id="edit_user_1">
<div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div>
</form>
Now let’s look at the #new and #edit forms for the admin section of the site.
1 2 3 |
<% form_for [:admin, @user] do |form| %> <% end %> |
This will generate the same html as above except with ’/admin’ being prefixed onto each action attribute value.
1 2 |
[:admin, @user] |
Ooo that is ugly.
Some more examples.
1 2 3 4 |
redirect_to [:admin, @user] # redirect_to admin_user_url link_to 'show', [:admin, @user] # link_to 'show', admin_user_url |
This anonymous Array syntax seems like such a quick hack, like they forgot about namespaced controllers. With all the nice interfaces in Rails, this one really sticks out. I’m willing to bet this becomes deprecated in the near future.
In the meantime I’m going to stick with the much better looking old style.
1 2 3 |
<% form_for @user, :url => admin_user_url do |form| %> <% end %> |
However, this sucks because I’d really like to take advantage of these smarter #form_for, #redirect_to and #link_to versions but I consider consistency in my code more important. I’d rather see 1 consistent way of using named routes instead of using them solely in the namespaced controller sections of an app in order to avoid this new ugly syntax.
This would probably be a lot cleaner in php.
About this entry
You're reading an entry on GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS, the company weblog of thoughtbot, inc.
- Author:
- Jared Carroll
- Published:
- January 21st 01:04 AM
- Updated:
- January 21st 09:50 AM
- Sections:
- Technology
thoughtbot is hiring
We are hiring web developers and web designers in both Boston and New York, NY.
What are we up to?
We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.

Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.
About thoughtbot, inc.
We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.
2 comments
Jump to comment form