当我做的
rails g model user name:string rails g controller users index create new destroy show
并编辑config / routes.rb来添加:
resource :users
捆绑执行rake路线给出:
users POST /users(.:format) {:action=>"create",:controller=>"users"} new_users GET /users/new(.:format) {:action=>"new",:controller=>"users"} edit_users GET /users/edit(.:format) {:action=>"edit",:controller=>"users"} GET /users(.:format) {:action=>"show",:controller=>"users"} PUT /users(.:format) {:action=>"update",:controller=>"users"} DELETE /users(.:format) {:action=>"destroy",:controller=>"users"}
但是,当我做
rails g resource users name:string
(它自动添加资源:用户到config / routes.rb)
捆绑执行耙子路由
我得到
users GET /users(.:format) {:action=>"index",:controller=>"users"} POST /users(.:format) {:action=>"create",:controller=>"users"} new_user GET /users/new(.:format) {:action=>"new",:controller=>"users"} edit_user GET /users/:id/edit(.:format) {:action=>"edit",:controller=>"users"} user GET /users/:id(.:format) {:action=>"show",:controller=>"users"} PUT /users/:id(.:format) {:action=>"update",:controller=>"users"} DELETE /users/:id(.:format) {:action=>"destroy",:controller=>"users"}
所以我的问题是
当我生成一个控制器,我该如何获得正确的帮助方法
link_to’Destroy’,instance,:method => :删除
工作?
因为目前它给出错误user_path没有定义.
解决方法
你应该打电话
rails g controller user index create new destroy show
代替
rails g controller users index create new destroy show
为了获得资源:用户给你想要的帮手.
后者导致Rails假定用户是一个单一的对象,资源:用户应该创建一个所谓的单一资源:
http://guides.rubyonrails.org/routing.html#singular-resources
因此,user_path未定义,而users_path已定义.