views:

167

answers:

3

I am trying to create clean urls in rails. I have installed PermalinkFu and I can generate the links just fine. What I would like to do is keep my RESTful resource. Is there a way I can overload:

map.resources :location

to return :permalink variable instead of :id?

Similar to how I can create:

map.connect 'location/:permalink' :controller => 'location', :action => 'show'

A side note: What is the best practice for handling permalinks, should they only used to display the view/show action then use the :id for edit/destroy/etc? And should I even be concerned with the controller using :id instead of :permalink as a variable name?

A: 

Looking for an answer to this question as well. :path_prefix seems to be the thing that should solve this problem, but I don't understand how it should be used.

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/7fc46fd8e354792b/3e364626d9d67403?lnk=gst&q=restful+two+parameters

Kris
+2  A: 

It's my way to solve this problem:

use to_param method in your model Location:

def to_param
  permalink
end

and in your routes.rb

map.resources :location

There is no need to add a rule for permalink

then you can find in your controller like this

@location = Location.find_by_permalink params[:id]

Hope it works.

Yi-Ru Lin
This is exactly what you do.
Brian Hogan
Right, the above is what I have now and am trying to change. This doesn't seem sloppy to anyone? Using the params[:id] variable when you are really passing params[:permalink]? What I'd like to do is have the map.resources :location populate the params[:permalink] var instead of params[:id]
Nick Faraday
Personally I think it's a very minor concern.
John Topley
This has always seemed sloppy to me. I've been grumbling about it for forever. :)
Ian Terrell
@Ian ya right now I'm just runing with params[:id], I don't like it but it works.
Nick Faraday
A: 

Is there a reason why you can't override the to_param method in your Location model? Ryan Bates has a Railscast that explains how to do this.

If you want to be a purist then you should use the permalink for all the actions, but it's undoubtedly more convenient to use the numeric ID for the edit and destroy actions. I guess it depends if you're bothered by people viewing the HTML and being able to see the actual primary keys of your records. Some might consider that a security hole.

John Topley
see comment above.
Nick Faraday