我是编程新手,已经学习Ruby on rails大约11周了。
在简单的" To do"上运行功能规范(使用RSpec和Capybara)时列表应用。我收到这个错误:
Failures:
1) User creates ITEM Successfully
Failure/Error: click_button 'Save'
ActionController::RoutingError:
No route matches [GET] "/items"
这是我的路线档案:
devise_for :users
resources :items, only: [:new, :create, :show]
get 'about' => 'welcome#about'
root to: "welcome#index"
end
...这是"项目"控制器:
class ItemsController < ApplicationController
def new
end
def show
end
def create
@item = Item.new(item_params)
@item.save
redirect_to @item
end
private
def item_params
params.require(:item).permit(:body)
end
end
项目模型:
class Item < ActiveRecord::Base
belongs_to :list
scope :unfinished, -> { where('done' => false) }
scope :unfinished_and_recent, -> { unfinished.where("created_at > ?", Time.now-7.days) }
scope :finished, -> { where('done' => true) }
validates :body, length: { minimum: 5 }, presence: true
validates :list, presence: true
end
规范:
require 'rails_helper'
feature 'User creates ITEM' do
scenario 'Successfully' do
visit new_item_path
fill_in 'Body', with: 'washcar'
click_button 'Save'
end
end
和项目工厂:
FactoryGirl.define do
factory :item do
body 'itembody'
list
end
end
......以下是表格:
<h1>Items#new</h1>
<%= form_for Item.new do |form| %>
<%= form.text_field :body, placeholder: 'Body' %>
<%= form.submit 'Save' %>
<% end %>
我跑了rake路线:
items POST /items(.:format) items#create
new_item GET /items/new(.:format) items#new
item GET /items/:id(.:format) items#show
about GET /about(.:format) welcome#about
答案 0 :(得分:2)
也许试试:
resources :items, only: [:new, :create, :show, :index]
如果您在命令行运行rake routes
,它应该显示您的应用可用的路由(并帮助解释上述内容)。
另外,如果您还没有读过它: