Le 06.10.12 13:19, Nickolas a ï¿½crit :
There is a function "resources" in the class "SingletonResource" that provides 7 routes for an indicated resource, like this:
Also, there are some gems(as "friendly_id") that give a possibility to store an arbitrary string in :id parameter, what helps to have frendlier links like /photos/big-ben.
But for several reasons this approach makes me feel odd. The fact that in the :id is stored, for example, a name of a model is not quite elegant. And also it leads to such code in controller:
@photo = Photo.find_by_name(params[:id])
Well, you as a developer may choose what the :id placeholder will contain. It's not always the 'name' of the model but any _identificator_ (a string one also) that will tie with your record.
When using friendly_id, your controller code will be exactly the same as without the gem (it is its purpose)
@photo = Photo.find(params[:id])
Now in the params hash the user may have put either a numeric id OR a friendly id (generally the user don't write that, the route generation will do).
In the context of friendly_id both are correct.
Which can be hardy called beautiful. "params[:name]" should fit much better. To avoid this, the gem mentioned above redefines the "find" method in choosen model, but I do not like it too. It can easily provoke gem conflicts, because there is a lot of gems that do it too. E.g., commonly a gem which adds "soft delete" also redefines this method that makes impossible to use them together or can break functionality somehow later.
There's nothing wrong with params[:id] containing "rails-tutorial", this is a controller concern, the semantic is: "We have a request to find a resource identified with params[:id]". Nowhere in Rails we say that this id IS an ActiveRecord record's id attribute. Of course for convenience and when using the whole stack, this last statement is what we want, but it's our controller code that controls this semantic.
For the issues of having multiple gems patching rails, this is another issue (or non issue when these gems are well written)
(BTW, for soft deletes, use scopes instead)
So, my suggestion is to add a :param_name option to "resources" method so we can have the behavior like this:
resources :photos, :param_name => 'name'
Could it be useful if I add such feature? In addition, there is a whole bunch of questions on stackoveflow with the same desire.
I'm -1 on this.