1

为什么我的测试:

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 %>
4

4 回答 4

1

你应该有

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

在你看来

于 2013-09-06T13:26:32.280 回答
1

看看你的路线。正如测试表明的那样,没有 POST 到 todos/new 的路径。您需要 POST 到 todos/ 并在控制器中的创建操作中处理它。

于 2013-09-06T13:32:40.840 回答
0

检查您是否无意中在表单中添加了 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| %>

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

于 2014-02-21T08:27:15.777 回答
0

在表单字段中添加路径将重定向到帖子路径

<%= form_for :todo do, url: todos_path  |f| %>
于 2019-05-31T17:26:59.107 回答