我不断收到这个奇怪的错误,即使一切都在localhost上运行。
1) User pages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `model_name' for NilClass:Class
# ./app/views/shared/_create_list.html.erb:2:in `_app_views_shared__create_list_html_erb__3760252191913263716_70266394394440'
# ./app/views/users/show.html.erb:17:in `_app_views_users_show_html_erb__4038196524711190952_70266368588040'
# ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'
users_controller
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update]
before_filter :correct_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
@lists = @user.lists
@list = current_user.lists.build if signed_in?
end
lists_controller
class ListsController < ApplicationController
before_filter :signed_in_user
def create
@list = current_user.lists.build(params[:list])
if @list.save
flash[:success] = "List created!"
redirect_to current_user
else
redirect_to current_user
end
end
_create_list partial
<div class="white-box">
<%= form_for(@list) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :name, placeholder: "Create new list..." %>
<%= f.submit "Create list", class: "btn btn-primary" %>
</div>
<% end %>
</div>
用户/显示
<div class="span8">
<% if @user.lists.any? %>
<h3>Lists (<%= @user.lists.count %>)</h3>
<ol class="lists">
<%= render @lists %>
</ol>
<% end %>
<%= render 'shared/create_list' %>
</div>
user_pages_spec
require 'spec_helper'
describe "User pages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
let!(:m1) { FactoryGirl.create(:list, user: user, name: "Foo") }
let!(:m2) { FactoryGirl.create(:list, user: user, name: "Bar") }
before { visit user_path(user) }
我对rails很新,这个错误一直困扰着我好几天!任何建议都非常感谢!
答案 0 :(得分:0)
此问题是因为如果用户登录,则将对象分配给@list
,否则它将仅为零。
要解决此问题,请在(if @list
)
users/show
<%= render 'shared/create_list' if @list %>