Ruby on Struts

September 6, 2008

Once upon a time, there was a web MVC framework called Struts. Struts was one of the original catalysts of web frameworks based on the MVC pattern, but it was written in Java and required copious amounts of XML to configure your application. One of the many things defined in the XML were the "Action Mappings". An Action Mapping essentially mapped a specific URL pattern to a specific Java class that would be responsible for handling that request.

Then came Ruby on Rails, which eliminated the need for these XML configuration files by using Convention Over Configuration. The way this works in Rails is that if a request is sent to the url /users/new, Rails will call the new method of the UsersController class to handle the request. Rails has a feature called routing that is used to map unconventional url patterns to specific controller actions. Then came RESTful Rails and the convention over configuration was gone.

With RESTful Rails, the request path simply represents what you want to perform an operation on, the Resource, and the HTTP method specifies what you want to do. This all makes sense, but one problem is that RESTful urls do not conform to the url convention. This means every action must be defined in the routing. A shortcut was added to the routing to allow one expression to define the 7 typical methods the controller for a resource will have, but any additional actions must be explicitly defined.

I've been recently working with an experienced Java developer with some familiarity Rails. He was confused by the way RESTful routing works and what paths the named route methods would generate. When he asked what benefits all this provides over the original /controller/action/id pattern, where only non-standard routes had to be mapped, I struggled to find any. I realized that I was doing this just because it was now the "Rails Way". He said this reminds him of Struts, and after some arguing and thinking about it, I realized he was right.

So it seemed unbelievably coincidental that we both found ourselves together today in a talk titled Unconvental Wisdom by Bruce Tate. In today's talk, Bruce pointed out that RESTful Rails adds complexity to Rails, which makes it harder to explain how Rails works to newcomers to Rails. I'm very interested to see how this talk is received by the Rails community, so if you weren't at the talk today, look for it on Confreaks in the next few weeks.

So looking back on RESTful Rails applications that I've developed, if they reach even a level of medium complexity, you end up with 50 lines or so in the routes.rb, with at least a handful of nested and custom routes, in addition to all of the resources. Imagine this as the admin interface for a simple blog:

map.namespace(:admin) do |admin| 
  admin.resources :articles, :has_many => :comments, :member => { :publish => :post }
  admin.resources :categories, :has_many => :articles
  admin.resources :comments, :belongs_to => :article
  admin.resources :tags, :has_many => :articles
end

If you are an experienced Rails developer, you can decipher this right away. But is this really easy understand? Or wouldn't convention over configuration be easier:

map.connect "/admin/:controller/:action/:id"

And what about named routes like new_admin_article_comment_path(@article)? Is that really more clear than admin_path("/comments/new", :article_id => @article)"? Do you really care if the urls are /comments/new?article_id=1 or /articles/1/comments? Maybe I missing something, but I'm starting to like the sound of idea of having almost nothing in my routes and not calling dynamically generated methods everywhere to just to build simple paths. After all, no code is faster than no code.

Posted in Technology | Tags Rails, Ruby, REST

Comments Comments Feed

1. Hi,

for me admin_path("/comments/new", :article_id => @article) isn't DRY, if someday you want modify the url 'comments/new' you're screwed (it's not the case with named routes, where you can just redefine map.new_admin_article_comment). And I really care if the urls are /comments/new?article_id=1 or /articles/1/comments, it's very very important :-)

# Posted By Thibaud Guillaume-Gentil on Saturday, September 6 2008 at 3:12 AM

2. +1, it does matter what URL do you have. I don't like /comments/new?article_id=1, it's ugly

In your blog you have URLs like http://paulbarry.com/articles/{year}/{month}/{day}/{permalink}
Why? Why not http://paulbarry.com/articles?id=111 ?

URLs looks better without question marks, believe me. Also I agree with Thibaud, when you decide to change URLs, you will have to change them in a single place.

# Posted By Dmytro Shteflyuk on Saturday, September 6 2008 at 3:53 AM

3. In February I was busy trying to understand and convert a coworker of mine to RESTful Rails, because I wanted to stay at the top of the curve and keep track of these things. And besides, RESTful Rails seemed to provide a number of important benefits.

I became familiar with make_resourceful and started using that in my projects. I was really pleased, because I could replace ~100 line controllers with ~20 line controllers, nevermind that I always wound up overriding current_objects and certain other methods to get it to do what I want.

This past week I found myself working on an old Rails 1.2 project, maintaining some things and I needed to add an RSS feed. I started putting all that RESTful stuff into the controller and the routing and then I thought, what the hell, I'll just make a little method and a little template. Twenty minutes later, it's completely done and linked to from the main template. How cool was that! And then I sat there thinking, wait a second, have I ever really *used* the fact that my RESTful methods could handle different "types" of responses? Or have I only ever used it to handle HTML this whole time?

So I think the one case where it would really have been handy to have been using REST came and left. I used respond_to inside a regular method and there still are no resources in this application. Reading this blog entry certainly makes me wonder. I had been thinking about how I need to spend some time modernizing this application, how I should go through and stick make_resourceful inside all of the controllers and really fatten up that routing. And yeah, it's probably a savings to remove 80 lines of code from 10 or 12 controllers and add 50 to the routing. But what about all those links? My app is going to have deeply nested resources. Already in another app, I had things like edit_admin_user_role_permission_path. Is that really cleaner than :controller => '/permissions'?

So your article really resonates with me. REST is great if you're building an API, but aren't we mostly still building websites? How much more work should we be willing to do? And why do we do things that are "correct" when they make other things so much worse?

I had been investigating the other Ruby frameworks and discounting some of them because of a general lack of REST, but maybe REST isn't what I need. And I had encountered an error relating to REST (I had an ID with a period in the name and it wanted to break that into an :id and a :format parameter). Maybe I'll just scrap REST for the time being and forget about the pretty URLs. Or make the pretty URLs without using map.resource.

# Posted By Daniel Lyons on Saturday, September 6 2008 at 4:02 AM

4. I spent most of my time working on an Rails app originally developed for 1.2, and hence theres no RESTfulness. At various times on other projects I've tried to use the REST style but always come back to it seeming more complicated than my companies 'old' rails app. I came to rails originally because I liked the simplicity and neatness - REST definately seems to take some of this away.

As for pretty URLs, this easily is solvable for the important user facing URLs in the routes.rb file - and you get the URLs you want then as well.

# Posted By Jebw on Saturday, September 6 2008 at 5:39 AM

5. The nice thing about the RESTful routing is once you start using polymorphic URL helpers, and liberal application of to_s and to_param, along with nested resources. You can end up with some wickedly beautiful URLs, along with nice and clean template code generating your links.

Suddenly, your links are only

<%= link_to comment, [ article, comment ] %>

That'll use to_s for the text of the link, and it'll find the route for a the article, and then its nested comment.

You don't have to know or care about the names of your routes.

Same goes for the RESTful actions. To delete a comment, you do something similar to

<%= link_to comment, [ article, comment ], :method=>:delete %>

Nice and smooth.

# Posted By Bob McWhirter on Saturday, September 6 2008 at 9:23 AM

6. @Jebw

I agree, use routing to define pretty URLs for public facing things, but depending on what your app does, there may be lots of URLs that have no benefit to being pretty, because they are only seen by administrative users.

# Posted By Paul Barry on Saturday, September 6 2008 at 9:51 AM

7. I'll admit that using RESTful controllers does fatten up your routing a bit, but I think that ultimately this makes the entire app come closer to "convention over configuration", not farther away from, because in all your controllers you have (mostly) the same set of methods and you can depend on them all to do the same thing to the resources they are in charge of. Maybe there is a bit more of a learning curve (in fact, I know there is), and I'm not saying you want to use it for everything (I have several ajax-y actions in my app that I don't think really relate to resources, and so I use a traditional rails route for them), but ultimately I think the extra layer is a net gain.

# Posted By Ethan Vizitei on Saturday, September 6 2008 at 11:30 AM

8. Ironic. I had the same reaction to RESTful Rails but only as a found the routing annoying when I first saw it. Glad that you captured this

BTW, your blog hates iPhone

# Posted By Evan Light on Saturday, September 6 2008 at 9:59 PM

9. NM. It hates NetNewsWire on iPhone.

# Posted By Evan Light on Saturday, September 6 2008 at 10:01 PM

10. Hey Paul, your article really resonated with me. As previous commenters have mentioned, RESTful routes are great when you need a standard resource-based API (which we commonly do as we do a lot of Rails CMS's with Flex front-ends, and this works wonderfully). However, I find myself often needing to leave the confines of RESTful controllers whenever I need to do something.....well, interesting.

To illustrate (with a very basic example), let's look at reporting interfaces. A basic report takes some user inputs and returns an output. We're really talking about two actions. I suppose you could squeeze this into the 'new' and 'create' actions, but really, this is bastardizing the tenets of REST. Furthermore, I have no need whatsoever for edit/update/destroy/index/show. I suppose this is handled by a combination of RESTful and default routes.

But I really was happy to see someone else put this out there, as it has bothered me a bit as well. I guess I just feel that there's a lot of hoopla around the idea of "everything should be RESTful" that seems to be a little misguided. Then again, maybe I'm just misinterpreting the community consensus....

# Posted By John Trupiano on Sunday, September 7 2008 at 10:58 AM

11. I don't think Rails prevents you from doing unRESTful routings. You can still fall back to what you use to do.

# Posted By Fadhli on Sunday, September 7 2008 at 10:19 PM

12. RESTful routing in Ruby on Rails breaks:
a)Convention over Configuration principle and
b)Don't Repeat Yourself principle.

RESTful URLs are good but someone needs to come up with a smarter routing implementation that uses route mapping conventions.

Current implementation of RESTful routing is particularly severe on beginner Rails programmers.

# Posted By tabrez on Tuesday, September 9 2008 at 8:05 AM

13. I totally agree with your thoughts on this.

REST is overhyped. It's annoying that most of the Rails community blindly follows the newest convention without even asking why.

My two major apps are non-REST and doing just fine.

# Posted By Seth on Tuesday, September 9 2008 at 9:27 PM

14. I like REST and RESTful routing. You can't shoe horn every problem on top of if it, so you can choose where you want to me.

As for admin_path("/comments/new", :article_id => @article)... this isn't very clear. And worse you now have a hard coded path in your application.

# Posted By bryanl on Friday, September 12 2008 at 7:09 AM

15. brian atwood maniac pumps the perfect wear-with-everything color. These wardrobe staples have 5" stiletto heels and hidden 1 1/2" seamless platforms. These Brian Atwood 'Loca' studded pumps are so ravishing, that I'd totally be willing to pop an aspirin before wearing them. And I really lovebrian atwood mesh sandals and Brian Atwood Asymmetric Patent Pumps.brian atwood nude patent leather katie lee pumps. Bold shoes like these sculptural purple suede Brian Atwood 'Lola' pumps make an outfit. purple suede Brian Atwood 'Lola' pumpswith a heel that measures approximately 140mm / 5.5 inches with a 30mm / 1 inch concealed platform. Brian Atwood pumps have a closed toe, cutout detail at arch with elasticated straps and a leather sole.
Herve Leger Dress shop specialises in superior Herve Leger Dress, provides hundreds of discount and fashion Herve Leger Dress, sexy Herve Leger Dress, Herve Leger Skirt, disount Herve Leger Dress-a professional Herve Leger Shop.

# Posted By Brian Atwood maniac pumps on Saturday, August 28 2010 at 6:09 AM

16. Wonderful brian atwood shoes for your style.BRIAN ATWOOD High-heeled sandals Purple is in stock in our store only one size in 36.5 EU. Elegant purple color, you will like it.
And these Pink Brian Atwood Maniac Patent Platform Pumps not only romantic and gorgeous too! It's patent leather leave us sunshine warmness and sexy!
When I original saw these brian atwood loca studded pumps in a metallic version, a while ago, I likeable them OK, but now I am going unhinged for the lavender version!
I really love brian atwood nico patent pump.
And this year, brian atwood maniac pumps are so hot among stars, I saw many times that Victoria-Beckham wearing them on the show time.
If you don’t know about brian atwood…You should. When everyone else is obsessing over the latest pair of Loubs or YS’L’s it’s very easy to overlook awesome shoe lines and designers who push the envelope. brian atwood is one of those designers.I’m a fan of his lines because Atwood really sets out to make his shoes a conversation piece. The use of alternative textures and fabrics makes brian atwood shoes unique. When wearing his shoes you are guaranteed a compliment from someone and a definate shoe in for best footwear. So ladies (and gents) get into these! And I know my card carrying “Shoe Wh*res” are going to love these!
Buy brian atwood shoes at http://www.brianatwoodcom.com. Buy brian atwood pumps at http://www.brianatwoodcom.com/brian-atwood-brian-atwood-pumps-c-65_67.html.
When I original saw these brian atwood loca studded pumps in a metallic version, a while ago, I likeable them OK, but now I am going unhinged for the lavender version!
When it comes to intricate detailing and superb craftsmanship, you know a pair of brian atwood lexia is worth the investment.
Back by popular demand... brian atwood maniac tan, the perfect wear-with-everything color. These wardrobe staples have 5" stiletto heels and hidden 1 1/2" seamless platforms. In a beautiful, very neutral nude tan.
brian atwood whip snake shoe with Exotic whipsnake hidden platform and back metal inset.
Take a walk on the wild side with Atwood Jagger printed knee-high boots. Style them knee high to add sass to an LBD or wear them with the fold-over flap turned up to work this season's hot over-the-knee trend.
brian atwood cage peep Price: $228. 140mm Covered heel. Rounded peep toe. 40mm covered platform. Interwoven cut outs around front. Ankle strap with adjustable metal buckle. Leather insole and lining. Leather sole. Made in Italy.
When it comes to intricate detailing and superb craftsmanship, you know a pair of brian atwood crystal court shoe is worth the investment.
Brian Atwood Platform Chain Pumps with A silver chain scales the lace-up back of this vamped-up pumps.
brian atwood snakeskin platforms is the classic and the elastic heel detail was used again from last Fall.
brian atwood patent leather and mesh Futuristic patent leather style with mesh details and contrast stitching.
Heel measures approximately 95mm/ 3.5 inches. Let your feet do the talking in brian atwood dorota shoes. Wear this chic style with off-duty denim or to liven up office looks.

# Posted By Brian Atwood loca studded pumps on Saturday, August 28 2010 at 6:10 AM

17. A grand wedding on the beach may be the common dream of most single girls. The bright sunshine, the blue sky and the open sea can satisfy all the demands of the romantic girls. Of course, everything will be perfect with a beach wedding dresses.
Every woman deserve the most beautiful wedding dresses at her special day!The most beautiful smile in the word is belong to the brides when she find her wedding dresses!Love is the time when you slows down in front of the shops selling wedding dresses.
Make the day of your engagement a special one with a A-line or princess wedding dresses!Step like a goddess in tea- length ball gowns,formal ball gowns,victorian ball gowns,debutante ball gowns,prom ball gowns, contemporary ball gownsand vintage ball gowns available in chiffon, organza, silk and satin only at weddingdressesnow.com.
Congratulations! Your little daughter is getting married! But being the mother of the bride (or MOB) is not an easy role. Once upon a time mothers of the bride did most of the wedding planning, and thus got to have their own dreams realized. First of all, choose one from the perfect mother of the bride dresses for yourself here and to be the most elegant mother of bride!
Flower girl dresses must be the perfect accessory for the bride's dress. The flower girls look like the bells ring in the wedding announcements as they toll down the aisle one by one or hand in hand. If you are having flower girls at your wedding, take a look at these beautiful selection of Flower girl dresses from http://www.weddingdressesin.com.

# Posted By A line wedding dresses on Saturday, August 28 2010 at 6:11 AM

18. Jordan retro
new jordans
sneakers jordan
cheap jordan
jordans sale

jordan 1
jordan 2
jordan 3
jordan 4
jordan 5
jordan 6
jordan 7
jordan 8
jordan 9
jordan 10
jordan 11
jordan 12
jordan 13
jordan 14
jordan 15
jordan 16
jordan 17
jordan 18
jordan 19
jordan 20
jordan 21
jordan 22
jordan 23
jordan 2009
jordan 2010

air jordan retro 1
air jordan retro 2
jordan retro 3
jordan retro 4
jordan retro 5
jordan retro 6
jordan retro 7
jordan retro 8
jordan retro 9
jordan retro 10
jordan retro 11
jordan retro 12
jordan retro 13
jordan 14 blue
jordan retro 15
jordan retro 16
jordan retro 17
Jordan retro 18
jordan retro 19
air jordan retro 20
jordan retro 21
jordan retro 22
jordan retro 23
jordan retro 2009
jordan retro 2010

# Posted By sneakers jordan on Monday, August 30 2010 at 7:53 AM

19. Shop popular stores to find juicy couture women's

fashions on sale - all in one place. Find it all here: JC handbags, shoes, tracksuits,

clothing, and more!mac cosmetics
mac makeup
christian louboutin shoes
christian louboutin
links of londonWhat a perfect thing, it attracted

all the attention, the miraculous birth of boundless charm. It is worthy of

appreciation and ownership.

# Posted By 59.32.56.46 on Monday, August 30 2010 at 9:35 AM

20. Diamond earrings and tiffany silver bracelets can be a romantic gift for any occasion. These can be as simple as a single stone silver tiffany earrings or as elaborate as chandelier silver tiffany earrings embellished with numerous stones. It is important to keep in mind the individual you are buying the gift for. If the recipient of the gift would like something that they can wear every day, tiffany promise rings are the way to go. However if the gift recipient enjoys getting dressed up for a night on the town or goes to formal affairs on occasion, the chandelier tiffany inspired rings are the perfect gift.

# Posted By tiffany inspired earrings on Tuesday, August 31 2010 at 2:21 AM

21. Find ugg and Australian sheepskin boots, learn the history behind the Ugg boot, and when and what to wear with Cheap ugg boots, as well as cleaning and caring forAuthentic Ugg boots,Cheap ugg cardy boots,20%-40% high discount

# Posted By p on Wednesday, September 1 2010 at 2:03 PM

22. Find ugg and Australian sheepskin boots, learn the history behind the Ugg boot, and when and what to wear with Cheap ugg boots, as well as cleaning and caring forAuthentic Ugg boots,Cheap ugg cardy boots,20%-40% high discount

# Posted By p on Wednesday, September 1 2010 at 2:03 PM

23. Find ugg and Australian sheepskin boots, learn the history behind the Ugg boot, and when and what to wear with Cheap ugg boots, as well as cleaning and caring forAuthentic Ugg boots,Cheap ugg cardy boots,20%-40% high discount

# Posted By juan on Wednesday, September 1 2010 at 2:04 PM

24. There are many people like to searching the famous brand shoes
online.
Nike Air Max
is always the best choice and you will fall in love when the first sight.As the most sought-after products,
the famous brand shoes
are not only fashionable but also practical.Nike Air Max which is one of the hottest shoes in the summer are available in a variety of styles, colors and materials.
Nike Air Max shoes
are designed for yourself.You can pick up a pair now at www.online268.com.

# Posted By online268 on Wednesday, September 1 2010 at 4:20 PM

25.
Cheap nfl jerseys are your best choice, and retail nfl apparell and drop shipping are accepted as well. Grade cheap nfl sports jerseys,Competitive price and Best service, We do hope nfl jerseys usa to do long-term business with every clients and help you improvefootball jerseys more benefit from your market.

# Posted By fewrewre on Wednesday, September 1 2010 at 6:47 PM

26.
There is no doubt that when looking for Cheap nfl jerseys to buy you still want nfl shop to get something buy nfl jerseys authentic. Now that you know that there are Cheap nfl jerseys outlets out there for you to purchase and they are high quality and authentic, and I think you can buy the best one.

# Posted By fewrewree on Wednesday, September 1 2010 at 7:14 PM

27. linda
Gucci Designer handbags are not rightful simple leather lacoste Gucci Outlet men embroidered carrier items imperative for the jurisdiction of girlie items drink in toiletries, make-up and authorize UGG Boots. No girlfriend! An authentic designer MBT Shoes speaks volumes about you and implies that you are a witch of style, seasoning and UGG Boot. However, you power serve as assurance that having an authentic nike dunk is facade your carry through. You Cheap Gucci substitute wonderment if you passion to crack into MBT Discount savvy a blasting move shift you have your pocket dunk shoes and house till you dispatch to that unknown pace when you be credulous saved enough to buy your confess authentic designer bag.

# Posted By nike dunk on Thursday, September 2 2010 at 2:08 AM

28. Moncler Gamme Bleu series seems to be bound up with bike culture.Moncler This not only shows in its design but also the pattern printed in the clothes. In this series, designer put in leggings to match some clothes. herve leger This point arouses great controversy in public. It is common that ladies wear leggings to match their long shirts or short dresses.Moncler Jackets If a man put on a legging, he looks a little feminization.Hermes Birkin Somebody do not agree with this. They thought men also should be fashionable.Buy Moncler Wearing leggings is not the right of ladies, but also the fashion of men`s.Christian Louboutin In recent years, many clothes have been more neutralize. Men are paying more and more attention to their dressing.Moncler Clothing Creations in men`s dressing are more and more diversified. Fashion is such kind of thing that at first it will arouse controversy and with acceptance from more and more people it will be popular.Herve Leger dresses Maybe one day there will be more neutralized clothes that men and women can try together.Christian Louboutin Pumps

# Posted By moncler on Thursday, September 2 2010 at 2:55 AM

29. louis vuitton
louis vuitton handbags
louis vuitton handbag
louis vuitton bags
louis vuitton bag
replica handbags

# Posted By ss on Thursday, September 2 2010 at 7:30 AM

30. louis vuitton
louis vuitton handbags
louis vuitton handbag
louis vuitton bags
louis vuitton bag
replica handbags

# Posted By ss on Thursday, September 2 2010 at 7:30 AM

31. louis vuitton
louis vuitton handbags
louis vuitton handbag
louis vuitton bags
louis vuitton bag
replica handbags

# Posted By ss on Thursday, September 2 2010 at 7:37 AM

Add a Comment

(If you leave this blank, your IP address will be displayed instead)

(Optional, will not be displayed on the site)