2016-01-20 27 views
0

我有一個Rails窗體來爲商店創建配置文件。起初,我已經處理了路由resources :stores,一切都很好。但是,我想將我的資源分配給管理範圍和「公共」範圍之間的商店。一旦我這樣做,表單提交停止工作,通過我錯誤'沒有路由匹配[POST]「/管理/存儲」'。我不是爲什麼發生這種情況清楚,因爲我以爲我分了我的資源,適當地...Rails創建映射到錯誤的路由

scope "/admin" do 
    resources :stores, only: [:index, :edit, :update, :destroy] 
end 

resources :stores, only: [:new, :create] 

這裏是控制器的動作......

,我在發現
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 

一件事Rails的路由信息​​是,創建POST操作被集中到與其他所有/管理前綴資源的store_path

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 

我不知道我理解爲什麼發生這種情況,任何HEL p將不勝感激。爲什麼當我在'public'路徑中添加:new和:create操作時,表單試圖發佈到管理路徑?

非常感謝。

+0

表單是否使用'form_for(@store)'? – max

+0

我不確定我是否理解;你問爲什麼'create'的'store_path'被列在另一個'store_path' HTTP方法下面?我的意思是,這個URL是正確的,它是一個'store_path',並且一切似乎都是正確的。 –

+1

這不是發佈到管理路線,它*顯示*你將發佈的路線。 –

回答

1

POST被路由到創建行動,您明確指出你就想離開該行動了create行動上resources呼叫

它改變這一點,它應該工作

scope "/admin" do 
    resources :stores, only: [:index, :create, :edit, :update, :destroy] 
end 

如果你想要使用當前命名空間(admin)以外的路由,您需要將url: store_path選項傳遞給窗體幫助器,並指定method: :post選項。

+0

感謝哥們!這是完美的。 – skwidbreth