我有一个respec测试。我有一个Sizes控制器,只是想创建一个新的大小。
当我运行测试时,我得到了这个错误,谷歌上没有太多关于它的信息。
看起来很简单,但我真的被卡住了。有人可以帮忙吗?
1) adding size allow a user to add a size
Failure/Error: visit sizes_path
ActionView::Template::Error:
undefined method `children' for nil:NilClass
我的测试如下
require "rails_helper"
RSpec.feature "adding size" do
scenario "allow a user to add a size" do
size = create(:size)
visit sizes_path
expect(page).to have_content("XXLarge")
end
end
这是工厂女孩
FactoryGirl.define do
factory :size do
title "XXlarge"
end
end
这是我的尺寸控制器
class SizesController < ApplicationController
def new
@size = Size.new
end
def create
@size = Size.new(size_params)
if @size.save
redirect_to root_url
else
render 'new'
end
end
def index
@sizes = Size.all
end
private
def size_params
params.require(:size).permit(:title)
end
end
这是新视图
<h1>Listing Of Sizes</h1>
<button type="button" class="btn btn-success"><%= link_to 'Create New Size', new_size_path %></button>
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Size</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<% @sizes.each do |size| %>
<tr>
<td><%= size.title %></td>
<td><%= link_to 'Show', size %><%= link_to 'Edit', edit_size_path(size) %><%= link_to 'Destroy', size, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
我的布局标题有子项。
<header class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">SAFSY</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1" >
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" >Womens</a>
<ul class="dropdown-menu" role="menu">
<% Category.find_by(name:"Women").children.each do |category| %>
<li> <%= link_to category.name, category %> </li>
<% end %>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" >Mens</a>
<ul class="dropdown-menu" role="menu">
<% Category.find_by(name:"Men").children.each do |category| %>
<li> <%= link_to category.name, category %> </li>
<% end %>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
<ul class="nav navbar-nav navbar-right">
<% if logged_in? %>
<li><%= link_to "Dash", users_path %></li>
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Items<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><%= link_to "New Item", new_item_path %></li>
<li><%= link_to "Edit Items", user_items_path %></li>
<li><a href="#">Mass Upload Items</a></li>
<li><a href="#">Upload Item Images</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Profile<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><%= link_to "View Profile", user_path(current_user) %></li>
<li><%= link_to "Edit Profile", edit_user_path(current_user) %></li>
</ul>
</li>
</ul>
<li><%= link_to "Messages", users_path %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Settings<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Edit Money Back Policies</a></li>
<li><a href="#">Edit Warranty Policies</a></li>
</ul>
</li>
<li><%= link_to "Log out", logout_path, method: "delete" %></li>
<% else %>
<li><%= link_to "Log In", login_path %></li>
<li><a href="#">Sign Up</a></li>
<% end %>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</header>
答案 0 :(得分:0)
解决方案令人尴尬。我已经编写了13个小时。我正在重定向到redirect_to root_url
它应该是sizes_path
def create
@size = Size.new(size_params)
if @size.save
redirect_to sizes_path
else
render 'new'
end
end