custom routes not work when defined as "member" in rails 3.2 resources

1.1k views Asked by At

We need to define 4 custom actions in rails 3.2 app. Those 4 actions take care of creating and edit customer login. In routes.rb, here was addition to routes.rb:

member do
  get :new_customer_login
  put :create_customer_login
  get :edit_customer_login
  put :update_customer_login   
end

rake routes shows the correct route:

 new_customer_login_user GET    /users/:id/new_customer_login(.:format)        authentify/users#new_customer_login
create_customer_login_user PUT    /users/:id/create_customer_login(.:format)     authentify/users#create_customer_login
   edit_cutomer_login_user GET    /users/:id/edit_cutomer_login(.:format)        authentify/users#edit_cutomer_login
update_customer_login_user PUT    /users/:id/update_customer_login(.:format)     authentify/users#update_customer_login

However when we fired up the spec and there is an no route error:

No route matches {:action=>"new_customer_login", :controller=>"authentify/users"}

What we did was to change the member to collection in routes.rb:

collection do
  get :new_customer_login
  put :create_customer_login
  get :edit_customer_login
  put :update_customer_login   
end

Amazingly, the no route error disappeared and the debug hits the new_customer_login defined successfully. We have no clue why collection instead of member routes work. Those 4 actions are really working on a single record. Can someone provide reasoning why collection but not member works here and how to fix?

1

There are 1 answers

0
Dieter Pisarewski On
No route matches {:action=>"new_customer_login", :controller=>"authentify/users"}

This message actually indicates about insufficient arguments to build a url(id is missing).

Member actions are supposed to route to a single record and collection routes to a collection of records. That's why route helpers for member actions expect to receive a model object or an id as argument.

You should call new_customer_login_user(user) in your spec where user is a user object.

It also looks like you are defining standard CRUD actions and CustomerLogin is a resource in your project. In that case consider to use nested resources so rails will automatically generate them. See documentation at http://guides.rubyonrails.org/routing.html#nested-resources