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

と rake ルートが表示されます:

   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

あなたのルートを見てみましょう。テストが示すように、todos/new への POST へのルートはありません。todos/ に POST し、コントローラーの作成アクションでそれを処理する必要があります。

于 2013-09-06T13:32:40.840 に答える