路由错误 - 新路由没有匹配 [POST] [英] Routing Error - No route matches [POST] for new

查看:50
本文介绍了路由错误 - 新路由没有匹配 [POST]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到路由错误,但找不到问题出在哪里,我正在创建一个简单的 CRUD,但在使用 create 方法时遇到了这个问题.

I'm getting an error with routes and I can't find where is the problem, I'm creating a simple CRUD and get this trouble with the create method.

没有路由匹配 [POST] "/usuarios/new"

No route matches [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

<h1>Add new user</h1>

<%= form_for (:usuario) do |f| %>

<p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
</p>
<p>
    <%= f.label :idade %><br />
    <%= f.text_field :idade %>
</p>
<p>
    <%= f.label :email %><br />
    <%= f.text_field :email %>
</p>

<p>
    <%= f.submit "send" %>
</p>

<% end %>

推荐答案

正如 Flexoid 所指出的,您可能没有在控制器中添加 new 方法.

As Flexoid has pointed out, you probably haven't add the new method in the controller.

所以,把这个

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

这篇关于路由错误 - 新路由没有匹配 [POST]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆