Did you define the routes in config/routes.rb
?
您是否在CONFIG/routes.rb中定义了路由?
Rails.application.routes.draw do
resources :users
# ...
end
I got this error when in my config/routes.rb
, I mistakenly had user
(singular):
在我的配置/routes.rb中,我错误地将USER(单数):
resources :user
It needs to be users
(plural):
它需要是用户(复数):
resources :users
If you run the rake routes
command, you will find that there is no entry for users_path
in the very first column. What that means is that you have no route defined for action that will be called when the form is submitted.
如果运行rake routes命令,您会发现在第一列中没有USERS_PATH的条目。这意味着您没有为提交表单时将调用的操作定义路由。
If you want to use resources, then just add:
如果您想要使用资源,则只需添加:
resources: users
and if you want to use a different URL(route name)
for the users#new
action, then add
如果要为USERS#new操作使用不同的URL(路由名),则添加
resources: users, except: [:new]
below the route for users#new
in the routes.rb file.
在routes.rb文件中用户#new的路径下方。
And, if you don't want to go the resources way then add a new route:
而且,如果你不想去资源的方式,然后添加一个新的路线:
post 'users', to: 'users#create'
In your config/routes.rb
file add:
在您的配置/routes.rb文件中添加:
resources :users, except: [:new]
In your class UserController
, use:
在您的类UserController中,使用:
route: get /signup, to: 'user#new'
.
.
resources :users
where ":users
" is plural, because you will register multiple users.
其中“:USERS”是复数,因为您将注册多个用户。
This happens when we have a form and Rails doesn't know where to submit the form when we press the submit button.
For this, we need to go to routes and make sure we have resources:users
:
当我们有一个表单,而当我们按下提交按钮时,Rails不知道在哪里提交表单时,就会发生这种情况。为此,我们需要转到Routes并确保我们拥有资源:用户:
Rails.application.routes.draw do
#all the other resources and routes
resources :users
end
In one of the other answers, I saw
在另一个答案中,我看到
resources :users, except[:new]
Use this only if we have already introduced the new action in the routes:
仅当我们已在路线中引入新操作时才使用此选项:
resources :posts
get 'signup', to: 'users#new
resources :users, except[:new]
Based on your UserController, you should already have a route in the config/routes.rb file similar to this:
根据您的用户控制器,您应该已经在配置/routes.rb文件中有一个类似于以下内容的路由:
get 'users', to: 'user/#add'
In this case, Rails is going to look for a post users-route. So you need to define it too in the routes.rb file:
在本例中,Rails将寻找POST USER-ROUSE。因此,您也需要在routes.rb文件中定义它:
post 'users', to: 'user/#create'
To combine these two routes, you can simply add the following to your routes file:
要合并这两个路由,只需将以下内容添加到您的路由文件中:
resources: users
Which will automatically generate all the users related routes. You can have a look by typing in the terminal:
它将自动生成所有与用户相关的路线。您可以通过在终端中键入来查看:
rails routes
更多回答
By putting that line inside Rails.application.routes.draw
. Please read the routing guide
通过将该行放入Rails.Applation.routes.Drawing中。请阅读路线指南
I added resources :users, but now I have an error on username
我添加了资源:用户,但现在我有一个错误的用户名
"What is @user1 - how is it created? – DGM 11 mins ago"
“什么是@user1-它是如何创建的?-DGM 11分钟前”
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
您的答案可以通过其他支持信息来改进。请编辑以添加更多详细信息,如引用或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。
Welcome to SO! Please go through the Stack Overflow tour, read "How to Answer" and its linked pages, and "How do I format my posts...". Grammar is important on SO as SO is not a discussion board, it's more of a text book of programming Q&A.
欢迎来到So!请浏览堆栈溢出教程,阅读“如何回答”及其链接页面,以及“如何格式化我的帖子...”。语法很重要,所以它不是讨论板,它更像是一本编程问答的教科书。
我是一名优秀的程序员,十分优秀!