0

I'm trying to understand how to change this rule directly on the map.resources:

supposing I have a route:

map.resource :user, :as => ':user', :shallow => true do |user|
    user.resources :docs, :shallow => true do |file|
        file.resources :specs
    end
end

so I would have RESTful routes like this:

/:user/docs

/docs/:id

/docs/:doc_id/specs

So I see that is difficult to track the params[:doc_id] on this case because sometimes its params[:id] and sometimes its params[:doc_id] and in this case I would like to always call for one specific name so I won't have to create two different declarations for my filters.

Well, I did a little bit of research and I found this patch:

http://dev.rubyonrails.org/ticket/6814

and basically what this does is give you the ability to add a :key parameter on you map.resources so you can defined how you would like to reference it later so we could have something like:

map.resources :docs, :key => :doc ...

so I always would call the param with params[:doc] instead.

But actually this patch is a little bit old (3 years now) so I was wondering if we don't have anything newer and already built-in for rails to do this task?

P.S I'm not sure about that to_param method defined inside the model, apparently this didn't change anything on my requests, and on the logs I still getting: Parameters: {"doc_id"=>"6"} or Parameters: {"id"=>"6"} all the time.

zanona
  • 12,345
  • 25
  • 86
  • 141

1 Answers1

0

One method of making the parameters a little more friendly without writing fully custom routes is

# docs_controller.rb
def show
  @doc = Doc.find(params[:doc].to_i)
end

# doc.rb
def to_param
  [ id, permalink ].join("-")
  # assumes you're storing a permalink formatted version of the name of your doc
end

# routes.rb
map.resources :docs

This will give you URLs that look something like example.com/docs/234-the-name-of-your-doc

bensie
  • 5,373
  • 1
  • 31
  • 34
  • I see after posting that I'm not exactly answering your question. Rails does not have the functionality you're looking for built-in, the defaults of params[:id] pointing to the current resource is still the way to go. Parent records get the params[:parent_id] for consistency. For global filters outside the current resource, why not just pass the appropriate variable as a parameter to the filter? – bensie Nov 21 '09 at 18:45
  • yes, that's right, thanks bensie. but still, this thing about don't give you the possibility to change the resource key its kind of pointless isn't it? What would be the cons in this situation? I dont get it :\ – zanona Nov 21 '09 at 19:21