I am quite new to rails and even after reading the docs to routes I
still do
not manage to simply duplicate the new route:
<%= link_to ‘New Priority’, new_priority_path %> --> <%= link_to ‘Add
Priority’, add_priority_path %>
resources :priorities do
member do
get ‘add’
end
rake routes:
add_priority GET /priorities/:id/add(.:format) vs. new_priority GET
/priorities/new(.:format)
Is there any doc how the 7 default routes would be declared, especially
new
and create, if I would want to duplicate them (I don’t, I just want to
understand )
As mentioned above, I just want to understand how I would declare the 7
REST
routes if they would not have been declared for me, e.g. new1 (add in my
example), edit1, …
If I declare add as a collection route, this statement throws an error
<%= link_to ‘Add Priority’, add_priority_path %> (taken from
originally created code : <%= link_to ‘New Priority’, new_priority_path
%>)
<%= link_to ‘Add Priority’, add_priorities_path %> works as expected.
My original question was: what is my definition missing compared to the
original new?
Second, I find this very interesting: get ‘add’ => ‘priorities#create’,
:as
=> my_custom_add
In this form I get an error, the same with :my_custom_add.
Im trying to figure out what is it that you want. And if im not
mistaken,
you want to add priority to behave the same as new_priority.
when you declare resource , rails automaticly assign 7 route respect to
REST
the member method inside of the resource would apply to single instance
of
the resource
so you get for examlpe
when you define
member do
get ‘up’
get ‘add’
end
you will get:
/prioriteis/:id/add
but when you define
collection do
get ‘add’
end
you will get
/priorities/add
also if you dont specify aditional parameter, rails would create a route
name same as the get paramter
get ‘add’ would create add_priorities route and map it to priorites#add
add is referring to the get parameter and _priorities referring to the resource
you could also specify a parameter to custom map your route. for example
if
you want the add route is handled by create action on prioritiescontroller, then you could specify the method like this
get ‘add’ => ‘priorities#create’
you could also custom the name of the path using :as parameter
get ‘add’ => ‘priorities#create’, :as => my_custom_add
when you specify route inside resource, AFAIK
if you specify the route inside “collection”, rails will always append
ies
like priorities
if you specify the route inside “member”, rails will always use the
singular
word liake priority
so in that way, one solution (but i dont know if this the best way to do
it). is define custom route outside the resource block. such as.
resources :priorities do
end
get ‘/priorities/add’ => ‘priorities#new’, :as => :add_priority
this will give routes
priorities GET /priorities(.:format) {:action=>“index”,
:controller=>“priorities”}
POST /priorities(.:format) {:action=>“create”,
:controller=>“priorities”}
new_priority GET /priorities/new(.:format) {:action=>“new”,
:controller=>“priorities”}
edit_priority GET /priorities/:id/edit(.:format) {:action=>“edit”,
:controller=>“priorities”}
priority GET /priorities/:id(.:format) {:action=>“show”,
:controller=>“priorities”}
PUT /priorities/:id(.:format) {:action=>“update”,
:controller=>“priorities”}
DELETE /priorities/:id(.:format) {:action=>“destroy”,
:controller=>“priorities”}
add_priority GET /priorities/add(.:format) {:action=>“new”,
:controller=>“priorities”}
btw, im using rails 3, i don’t know if there’s different with the 2.x
version
In your example, add is like *new. *It should not be a member action,
it
is a collection one.
I think what you are asking for is a bit more elaboration of the default
REST routes.
When you do:
resources :priorities
This will generate the 7 default routes. which are two types:
Member (you are operating on a specific instance of the
priorities).
and those are: Show, Edit, Update, Destroy
Collection, Where you operate on prorities in general, but not an
existing one of them. which is :index, :new, :create
for member based actions, the paths generated in a singular form and
expects the ID to be passed. so
add_priority_path # wrong, you have not provided an id or an instance
of
Priority
add_priority_path(object) # should work, but this is not what u want.
new_priority_path is actually a collection. the route will be
/priorities/new
that’s interesting, apparently there is 3rd type other than collection
and
member that is specific to the new.
if u checked at
actionpack-3.0.3/lib/action_dispatch/routing/mapper.rb:953
you can how the default routes are added:
collection do
get :index if parent_resource.actions.include?(:index)
post :create if parent_resource.actions.include?(:create)
end
new do
get :new
end if parent_resource.actions.include?(:new)
member do
get :edit if parent_resource.actions.include?(:edit)
get :show if parent_resource.actions.include?(:show)
put :update if
parent_resource.actions.include?(:update)
delete :destroy if
parent_resource.actions.include?(:destroy)
end
I’m not sure if you can use the “new” on your own. try it and let me
know
how it goes