Reputation: 8454
I have a Rails form to create a profile for a store. Initially, I had handled the routing thusly resources :stores
and all was well. However, I wanted to divide my resources for stores between an admin scope and a 'public' scope. Once I did this, the form submission stopped working, passing me the error 'No route matches [POST] "/admin/stores"'. I'm not clear on why this is happening, since I thought I divided my resources appropriately...
scope "/admin" do
resources :stores, only: [:index, :edit, :update, :destroy]
end
resources :stores, only: [:new, :create]
Here are the controller actions...
def new
@store = current_user.stores.new
@store.build_address
@storeType = StoreType.all
end
def create
@store = current_user.stores.new(store_params)
if @store.save
flash[:notice] = "New store created"
redirect_to root_path
else
#ERROR STUFF
end
end
One thing that I noticed in the Rails route info is that the POST action for create gets lumped into the store_path with all of the other /admin prefixed resources
stores_path
GET /admin/stores(.:format) stores#index
edit_store_path
GET /admin/stores/:id/edit(.:format) stores#edit
store_path
PATCH /admin/stores/:id(.:format) stores#update
PUT /admin/stores/:id(.:format) stores#update
DELETE /admin/stores/:id(.:format) stores#destroy
POST /stores(.:format) stores#create
new_store_path
GET /stores/new(.:format) stores#new
I'm not sure I understand why this is happening, any help would be greatly appreciated. Why is the form trying to post to an admin route when I put the :new and :create actions in the 'public' route?
Thank you very much.
Upvotes: 0
Views: 227
Reputation: 619
POST
is routed to the create action, you specifically stated which actions you wanted leaving out the create
action on the resources
call
change it to this and it should work
scope "/admin" do
resources :stores, only: [:index, :create, :edit, :update, :destroy]
end
If you want to use a route outside your current namespace (admin) you need to pass the url: store_path
option to the form helper and also specify the method: :post
option.
Upvotes: 1