when I do
当我做
rails g model user name:string
rails g controller users index create new destroy show
and edit config/routes.rb to add:
并编辑config / routes.rb以添加:
resource :users
bundle exec rake routes gives:
捆绑exec 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"}
however, when I do
但是,当我这样做的时候
rails g resource users name:string
(which automatically adds resources :users to config/routes.rb) bundle exec rake routes
(自动添加资源:用户到config / routes.rb)捆绑exec rake路由
I get
我明白了
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"}
So my question is,
所以我的问题是,
when I generate a controller how can I get the correct helper methods to make link_to 'Destroy', instance, :method=> :delete work?
当我生成一个控制器时,我怎样才能获得正确的帮助方法来使link_to'Destroy',实例,:method =>:删除工作?
Because currently it gives an error user_path is not defined.
因为当前它给出了错误,所以没有定义user_path。
4 个解决方案
#1
11
You should call
你应该打电话
rails g controller user index create new destroy show
instead of
代替
rails g controller users index create new destroy show
in order to get resources :users
to give you the helpers you want.
为了获得资源:用户可以为您提供所需的帮助。
The latter causes Rails to assume that users
is a singular object, and that resources :users
should create what is called a singular resource:
后者导致Rails假定用户是单个对象,并且资源:用户应该创建所谓的单一资源:
http://guides.rubyonrails.org/routing.html#singular-resources
http://guides.rubyonrails.org/routing.html#singular-resources
as a result, user_path
is undefined, whereas users_path
is defined.
因此,user_path未定义,而users_path已定义。
#2
1
When you use rails g controller
and specify the method names, the generator only maps specific routes to the routes file. rails g resource
assumes that you want the whole resource functionality and will map resources
.
使用rails g controller并指定方法名称时,生成器仅将特定路由映射到routes文件。 rails g resource假定您需要整个资源功能并将映射资源。
In order to fix this, just go into your routes file and replace the specific mappings with a resources call.
为了解决这个问题,只需进入您的路由文件并用资源调用替换特定的映射。
resources :users
#3
0
What I really wanted was a way of creating a working (with correct delete/show paths) controller for an existing model (as described in the question) but just adding "resource :x" and generating the controller wasn't enough.
我真正想要的是为现有模型创建一个工作(具有正确的删除/显示路径)控制器的方法(如问题中所述),但只是添加“resource:x”并生成控制器是不够的。
I ended up using the scaffold_controller generator. It doesn't create any migrations or models, but it does generate a resource with views and rake paths
command shows the correct paths for delete and show to work.
我最终使用了scaffold_controller生成器。它不会创建任何迁移或模型,但它会生成带有视图和rake paths命令的资源命令显示删除和显示工作的正确路径。
#4
0
You can run following commands in the console:
您可以在控制台中运行以下命令:
$rails g model user name:string
$rails g scaffold_controller User
And add this code line to the file routes.rb:
并将此代码行添加到routes.rb文件中:
resources :users
#1
11
You should call
你应该打电话
rails g controller user index create new destroy show
instead of
代替
rails g controller users index create new destroy show
in order to get resources :users
to give you the helpers you want.
为了获得资源:用户可以为您提供所需的帮助。
The latter causes Rails to assume that users
is a singular object, and that resources :users
should create what is called a singular resource:
后者导致Rails假定用户是单个对象,并且资源:用户应该创建所谓的单一资源:
http://guides.rubyonrails.org/routing.html#singular-resources
http://guides.rubyonrails.org/routing.html#singular-resources
as a result, user_path
is undefined, whereas users_path
is defined.
因此,user_path未定义,而users_path已定义。
#2
1
When you use rails g controller
and specify the method names, the generator only maps specific routes to the routes file. rails g resource
assumes that you want the whole resource functionality and will map resources
.
使用rails g controller并指定方法名称时,生成器仅将特定路由映射到routes文件。 rails g resource假定您需要整个资源功能并将映射资源。
In order to fix this, just go into your routes file and replace the specific mappings with a resources call.
为了解决这个问题,只需进入您的路由文件并用资源调用替换特定的映射。
resources :users
#3
0
What I really wanted was a way of creating a working (with correct delete/show paths) controller for an existing model (as described in the question) but just adding "resource :x" and generating the controller wasn't enough.
我真正想要的是为现有模型创建一个工作(具有正确的删除/显示路径)控制器的方法(如问题中所述),但只是添加“resource:x”并生成控制器是不够的。
I ended up using the scaffold_controller generator. It doesn't create any migrations or models, but it does generate a resource with views and rake paths
command shows the correct paths for delete and show to work.
我最终使用了scaffold_controller生成器。它不会创建任何迁移或模型,但它会生成带有视图和rake paths命令的资源命令显示删除和显示工作的正确路径。
#4
0
You can run following commands in the console:
您可以在控制台中运行以下命令:
$rails g model user name:string
$rails g scaffold_controller User
And add this code line to the file routes.rb:
并将此代码行添加到routes.rb文件中:
resources :users