2012-05-08 34 views
1

我最近升级到了Rails 3.1(从3.0开始),出于某种原因,我的一条路由不再工作。我有一个表单发布csv文件,并由项目控制器处理。路线如下:Rails 3.1路由 - 集合不起作用

resources :items do 
    member do 
     post 'receive' 
     post 'show' 
    end 

    collection do 
     post 'csv_import' 
     get 'transactions' 
     get 'template' 
    end 
    end 

而这里是我在日志中看到的 - 它看起来像是发布了正确的操作。

Started POST "/items/csv_import" for 127.0.0.1 at Tue May 08 11:09:52 -0400 2012 
    Processing by ItemsController#show as HTML 

但它正在由show动作处理:

ActiveRecord::RecordNotFound in ItemsController#show 

Couldn't find Item with id=csv_import 

我不能为我的生命看到我在做什么错在这里。

+1

什么'rake routes'不得不说? –

回答

5

post 'show'线与此干扰,因为当你发布到/items/csv_import,轨道认为你的意思是items/csv_import/show,用csv_import是要导入的项目的ID。如果您运行rake routes,你会看到这样的一个部分:

  item POST /items/:id(.:format)  items#show 
csv_import_items POST /items/csv_import(.:format) items#csv_import 

这第一项。您的文章匹配/items/csv_import,它甚至从来没有碰到第二个。

您可以将member do ... end块移动到collection do ... end块之后,它应该可以正常工作。

但是,我只是建议摆脱post 'show'并将该方法重命名为更好的东西,因为它违背了标准的导轨/休息约定。

+0

好眼睛!另一个项目开发人员补充说,我没有看到它,而是看着它。谢谢!像魅力一样工作。 – ideaoforder