How can I change the default parameter names in Rails 3 routing?
In my Rails 3 project I have a list of routes like this:
resources :projects do
resources :studies
end
resources :sticky_notes
resources :study_templates
...
Currently by default the ids in the URLs from these routes can be called with params[:id], but I want to be able to call them with params[:sticky_note_id], 开发者_C百科params[:study_template_id], params[:study_id], etc. Is there a way I can specify the parameter name for the ID of these projects? Do I have to write each route out manually without 'resources'?
Thanks!
Edit: Here's an example of what i'm trying to do: This is what happens when the routes are defined as written above:
resources :projects do
resources :studies
end
# results in /projects/:project_id/studies/:id
# /projects/:project_id/studies/:id/edit
# /projects/:project_id/studies/:id/new
# etc.
resources :sticky_notes
# results in /sticky_notes/:id
# /sticky_notes/:id/edit
# /sticky_notes/:id/new
# etc.
This is what I want:
match '/projects/:project_id/studies/:study_id' => 'studies#show'
match '/projects/:project_id/studies/:study_id/edit' => 'studies#edit'
match '/projects/:project_id/studies/:study_id/new' => 'studies#new'
...
# results in /projects/:project_id/studies/:study_id
# /projects/:project_id/studies/:study_id/edit
# etc
match '/sticky_notes/:sticky_note_id' => 'sticky_notes#show'
match '/sticky_notes/:sticky_note_id/edit' => 'sticky_notes#edit'
match '/sticky_notes/:sticky_note_id/new' => 'sticky_notes#new'
...
# results in /sticky_notes/:sticky_note_id
# /sticky_notes/:sticky_note_id/edit
# etc
I want the second part, but without all that work on my already-huge routes file. :) is it possible?
after all 'routes.rb' is just a simple ruby file, so why not use ruby code and maybe even make a method to generate the necessary routes .. lets have a look at a simple example using an array of resources, if you want to use nested resources you might want to modify the method to use hash-chains in order to pass the resources you want to add a member:
def add_nested_resource(toadd=nil, controller=nil, resources=[])
return if toadd.nil? || controller.nil? || ressources.empty?
resources.each { |x|
resources x do
resources toadd, :controller => controller
end
}
end
add_nested_resource(:notes, "notes", [:resource1, :resource2, ..., :resourceX]
would be equivalent to
resources :resource1 do
resources :notes, :controller => "notes"
end
resources :resource2 do
resources :notes, :controller => "notes"
end
.
.
.
resources :resourceX do
resources :notes, :controller => "notes"
end
That way you could simply write a lot of routes with few effort. Within the notes_controller of course you might have to distinguish which resource has called on it, I usually add a hidden field in the according forms where I leave a 'classified' name of the object that nests the nested object ... like
<%= form_for ... someform for resource1... do |f| %>
...
<%= hidden_field_tag :nesting_object, "Resource1" %>
...
<% end %>
Hope this helps you through your trouble...
精彩评论