Rails Scaffold问题#undefined方法`edit_pais_path’

我创建了一个名为pais的脚手架(这是巴西葡萄牙语中的一个词,与该国家相同),我使用以下命令创建:

ruby script\generate scaffold pais name:string abreviattion:string 

首先,我将变形改为我当地的习语,就像那样:

 inflect.plural /^([a-zA-z]*)s$/i, '\1ses' #The plural of Pais is Paises 

当我试图在http:// localhost:3000 / paises上打开页面时,我收到以下错误:

 undefined method `edit_pais_path' for # 

提前致谢。

问题

"pais".pluralize导致"pais"

这对于选择制作News模型的人来说非常普遍。 Rails需要区分模型的单数和复数版本。

的routes.rb

 map.resources :pais, :singular => :pai 

现在你将使用

pai_pathedit_pai_pathnew_pai_path


另外

 map.resources :pais, :as => "goats" 

将为您生成这些路径:

 HTTP URL controller action GET /goats Pais index GET /goats/new Pais new POST /goats Pais create GET /goats/1 Pais show GET /goats/1/edit Pais edit PUT /goats/1 Pais update DELETE /goats/1 Pais destroy 

有关详细信息,请参阅guides.rubyonrails.org上的“外部指南”中的Rails路由

我对这一点进行了修改,我发现了一个可以帮助你更好的解决方案。

步骤1

生成脚手架之前 ,请确保在inflections.rb文件中有适当的变形。

 ActiveSupport::Inflector.inflections do |inflect| inflect.irregular 'pokem', 'pokemon' end 

第2步

现在你可以生成你的脚手架了

 [bruno ~/pokedex]$ script/generate scaffold pokem name:string 

第3步

看看你甜蜜的新路线!

 [bruno ~/pokedex]$ rake routes pokemon GET /pokemon(.:format) {:controller=>"pokemon", :action=>"index"} POST /pokemon(.:format) {:controller=>"pokemon", :action=>"create"} new_pokem GET /pokemon/new(.:format) {:controller=>"pokemon", :action=>"new"} edit_pokem GET /pokemon/:id/edit(.:format) {:controller=>"pokemon", :action=>"edit"} pokem GET /pokemon/:id(.:format) {:controller=>"pokemon", :action=>"show"} PUT /pokemon/:id(.:format) {:controller=>"pokemon", :action=>"update"} DELETE /pokemon/:id(.:format) {:controller=>"pokemon", :action=>"destroy"} 

注意

如果定义拐点之前生成脚手架,则不会更新命名路径。