繁体   English   中英

Rails-没有路线匹配[POST]

[英]Rails - No route matches [POST]

为什么我要测试:

feature "Manage todos" do
  scenario "create a new todo" do
    visit root_path
    fill_in 'Email address', with: 'junk@snap2web.com'
    click_button 'Sign in'
    click_link('Add a new todo')
    fill_in 'Description', with: 'Buy some milk'
    click_button 'Create todo'
    expect(page).to have_css 'li.todo', text: 'Buy some Milk'
  end
end

错误:

1) Manage todos create a new todo
  Failure/Error: click_button 'Create todo'
  ActionController::RoutingError:
    No route matches [POST] "/todos/new"

当我的路线有:

Todos::Application.routes.draw do
  root 'high_voltage/pages#show', id: 'homepage'
  resource :session, only: [:create]
  resources :todos
end

耙路显示:

   Prefix Verb   URI Pattern               Controller#Action
     root GET    /                         high_voltage/pages#show {:id=>"homepage"}
  session POST   /session(.:format)        sessions#create
    todos GET    /todos(.:format)          todos#index
          POST   /todos(.:format)          todos#create
 new_todo GET    /todos/new(.:format)      todos#new
edit_todo GET    /todos/:id/edit(.:format) todos#edit
     todo GET    /todos/:id(.:format)      todos#show
          PATCH  /todos/:id(.:format)      todos#update
          PUT    /todos/:id(.:format)      todos#update
          DELETE /todos/:id(.:format)      todos#destroy
     page GET    /pages/*id                high_voltage/pages#show

我的控制器有:

$ cat app/controllers/todos_controller.rb 
class TodosController < ApplicationController
  def index
  end

  def new
  end

end

表格具有:

$ cat app/views/todos/new.html.erb 
Add a new todo
<%= form_for :todo do |f| %>
  <%= f.label :description %>
  <%= f.text_field :description %>
  <%= f.submit 'Create todo' %>
<% end %>

你应该有

<%= form_for Todo.new do |f| %>

在你看来

看一下您的路线。 正如测试所暗示的,没有通往POST到待办事项/新的途径。 您需要发布POST todo //并在控制器的create动作中处理它。

检查您是否在表格中无意中添加了2个表格标签。 我是通过复制这样的引导代码片段来做到这一点的...

<div class="col-sm-8 contact-form">
  <form id="contact" method="post" class="form" role="form">
    <div class="row">

并将其包装在form_for标签中...

<%= form_for @contact, url: contacts_path do |f| %>

烦人的是,如果我手动单击按钮但测试失败,它可以正常工作

在表单字段中添加路径将重定向到发布路径

<%= form_for :todo do, url: todos_path  |f| %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM