简体   繁体   中英

Rails Scaffold problem # undefined method `edit_pais_path'

I created a scaffold of named pais (This is a word in Portuguese of Brazil and is the same that country), i created using the follow command:

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

First I changed the inflections to my local idiom, like that:

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

And when I tryied to open the page on http://localhost:3000/paises I'm receiving the follow error:

undefined method `edit_pais_path' for #<ActionView::Base:0x387fdf4>

Thanks in advance.

The problem

"pais".pluralize results in "pais"

This is really common for people that choose to make a News model. Rails needs to differentiate between the singular and plural version of your model.

routes.rb

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

Now you will use

pai_path , edit_pai_path , and new_pai_path


Alternatively

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

Will generate these paths for you:

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  

Checkout the Rails Routing from the Outside in Guide on guides.rubyonrails.org for more information

I tinkered around with this a little bit more and I found a solution that might help you a bit better.

Step 1

BEFORE you generate your scaffold, be sure to have the proper inflection in place in your inflections.rb file.

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

Step 2

Now you can generate your scaffold

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

Step 3

Check out your sweet new routes!

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

Note

If you generate your scaffold before your define your inflection, the named routes will not be updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM