路由错误 – 没有路由匹配 for new
我收到路由错误,我找不到问题所在,我正在创建一个简单的CRUD并使用create方法遇到麻烦。
错误
没有路线匹配[POST]“/ usuarios / new”
调节器
def new @usuario = Usuarios.new end def create @usuario = Usuarios.new(params[:usuario]) if @usuario.save redirect_to usuario_path, :notice => "Cadastrado realizado com sucesso!" else render "new" end end
new.html.erb
Add new user
正如Flexoid指出的那样,您可能没有在控制器中添加new
方法。
所以,把它
def new @usuario = Usuario.new end
编辑
你必须要多加注意。
看一看:
def new @usuario = Usuario.new # not Usuarios.new, that's wrong. end def create @usuario = Usuario.new(params[:usuario]) # not usuarios, first letter should be capital if @usuario.save redirect_to usuarios_path, :notice => "Cadastrado realizado com sucesso!" # usuario_path requires an id parameter like `usuario_path(@usuario)` or you could redirect to the `index` with `usuarios_path` else render "new" end end
更改
<%= form_for (:usuario) do |f| %>
至
<%= form_for (@usuario) do |f| %>
好像忘了配置Rails路由器了。
尝试将此添加到config/routes.rb
文件中:
resources :usuarios
作为参考,您可以从Outside In阅读rails rails Rails Routing 。