Rails脚手架问题 # undefined method `edit_pais_path'

4

我创建了一个名为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 #<ActionView::Base:0x387fdf4>

提前致谢。


只是一个小提示,Rails 是用英语编程的。如果你把模型保留在西班牙语中,那么很难完成许多事情。你可以在 routes.rb 文件中使用 :as 选项来更改名称为任何你想要的名称;-) - Hock
2个回答

3

问题

"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  

查看Rails Routing from the Outside in指南,获取更多有关it技术的信息。该指南位于guides.rubyonrails.org


1

我对这个问题进行了一些调试,找到了一个可能更好的解决方案。

步骤1

生成脚手架之前,请确保在您的 inflections.rb 文件中设置了正确的词形变化规则。

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

步骤二

现在您可以生成您的脚手架

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

步骤三

查看您的新路由!

[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"}

注意

如果在定义单词形态之前生成脚手架,那么命名路由将不会更新。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接