Url helpers, in rails…

Anyone’s who’s used rails already knows about helper functions. Basically, for those that don’t, they are functions you call in your views to output HTML. My question is, why!?

Take for example link_to (google to find out more, railsapi, which I just tried going to is giving an application error). In it you have the format of something like:

link_to “link name”, :controller_name, …

What does it output? an a-href tag linking to the controller. Now, some people may be thinking “hey this is cool” or “hey, I don’t see the problem”, but I’m kinda curious why we needed to redefine the a-href link. Some may say, so we don’t link to the specific controller (e.g. typing in /foo, which would be not DRY compliant) – in which case, they are exactly right. What I’m curious about though? Why not do it the way Django does it?

Take this example:
{% url myview.method id %}

now, you can also do it by names, but instead I’ll give the documentation for full reference:
http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#url

Why do I like this? It returns something like:

/foo/1, if the ID is 1, and foo is your view method.

Why do I like this better? Because it’s not attempting to redefine HTML, it returns the link, without redefining the HTML that goes with it. I don’t exactly understand why Rails decides it’s important to redefine the way a link works in this fashion.

I will give Rails the benefit of the doubt in that they probably thought this was a good idea and aren’t changing it to keep backwards compatibility. There are a number of helper methods, though – maybe I’m just not getting it, or maybe I’m just not cool enough to like everything about rails. I’m sure it’s a matter of time before I get someone flaming at me on my post here :) I definitely don’t feel any framework is the best – although I will admit with the Rails people I know, it’s almost always touted as being the definitive trophy that everyone should strive to reach. Anyone who doesn’t go for this trophy is foolish and wrong.

sigh

Edit: While mentioning this to a friend, I recalled what one person told me as the reason for the link_to. The reason was for the DRY principle. This basically means: Don’t Repeat Yourself. This is cool in all, but if you want 5 links, you’re going to do 5x a hrefs, or 5x link_to – the same basic stuff. If you want it in a loop of link_tos, you can do the same with a loop of ahrefs in Django. I don’t think the link_to really saves anything.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Comments are closed.