REST-less with link_to

This week, I file my first major complaint with Rails.

I was innocently running tests and tweakings some views – when I discovered function that should work longer did.

A little background, my app is filled with routes the make the URLs more contextually appropriate and memorable than the standard REST URL construction. If you have any Rails experience – you can see the problem with using the link_to helper

Rails likes to turn this:
< %= link_to 'Edit', :controller => 'items', :action => 'edit', :id => '1' %>
into this:

Now my standard routes are more like this:

If I do something like this, consistent with how my routes are structured:
< %= link_to 'Edit', :controller => 'items', :action => 'edit', :item_name => 'name_of_item_1' %>
link_to doesn’t pick up the item_name. If I throw other variables into the mix, say to pre-pop some attributes, link_to grabs it’s 3 perferred variables and forgets the rest.

Makes me feel like link_to is a lot like scaffolding: great for getting things up and running quickly, but something you want to clear out once you get serious.

3 thoughts on “REST-less with link_to

  1. link_to isn’t really the problem, it’s the way Rails is constructing the URL (this uses a method called url_for).

    Check out named routes for this. Then you can do link_to ‘some thing’, your_custom_path.

    Also, the RESTful routes have quite a bit of flexibility with how you name things. You can set prefixes and change the resource name (which is by default the model name). See:

    Also, you can replace the ID part of the URL with a name by overriding your model’s to_param method to return the name (however, then params[:id] will contain the name, so you have to change your find calls appropriately).

    You may also be interested in the permalink_fu or url_key plugins which automate some of this.

  2. Hi,

    I am pretty new to RESTful development, and generally for rails also.

    I am trying to make a “link_to” for listing tasks for a project. Normally the link_to should look like this…

    ‘tasks’, :action => ‘list_by_proj’ , :id => %>

    however, there is a compile error for this.

    Your problem seemed to be similar to mine, so I write directly to you… Am I doing some basic mistake or…

    thanks for ur time…


Comments are closed.