Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的方法`model#u name';错误_Ruby On Rails - Fatal编程技术网

Ruby on rails 未定义的方法`model#u name';错误

Ruby on rails 未定义的方法`model#u name';错误,ruby-on-rails,Ruby On Rails,我一直收到这个奇怪的错误,即使一切都在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.e

我一直收到这个奇怪的错误,即使一切都在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)>'

我对rails非常陌生,这个错误已经困扰了我好几天了!任何建议都将不胜感激

此问题是因为如果用户登录,则将对象分配给
@list
,否则它将仅为nil

要解决此问题,请在(
users/show
)中添加条件
if@list


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
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
<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>
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) }
<%= render 'shared/create_list' if @list %>