Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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 我需要当前用户只查看他的待办事项列表,并得到一个错误:nil:NilClass的未定义方法'lists'_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 我需要当前用户只查看他的待办事项列表,并得到一个错误:nil:NilClass的未定义方法'lists'

Ruby on rails 我需要当前用户只查看他的待办事项列表,并得到一个错误:nil:NilClass的未定义方法'lists',ruby-on-rails,ruby,Ruby On Rails,Ruby,我需要当前用户在登录后只查看自己的待办事项列表。使用Desive,我得到了nil:NilClass的错误未定义方法列表。 我在用户模型has\u many:List中进行了更改,以及在列表模型中所属的:User中进行了更改。 我在未定义nil:NilClass的方法'lists'时出错 列出控制器代码 class ListsController < ApplicationController before_action :authenticate_user!, except:

我需要当前用户在登录后只查看自己的待办事项列表。使用Desive,我得到了nil:NilClass的错误未定义方法列表。 我在用户模型has\u many:List中进行了更改,以及在列表模型中所属的:User中进行了更改。 我在未定义nil:NilClass的方法'lists'时出错

列出控制器代码

class ListsController < ApplicationController  
    before_action :authenticate_user!, except: [:index]

    def index
        @lists = current_user.lists.order('created_at') <----- error occurs here
    end 

    def new
        @list = current_user.lists.new
    end

    def create
        @list = current_user.lists.new list_params

        @list.save

        redirect_to lists_path
    end

    def edit
        @list = List.find params[:id]
    end

    def update
        @list = List.find params[:id]

        @list.update list_params

        redirect_to lists_path
    end

    def destroy
        @list = List.find params[:id]

        @list.destroy

        redirect_to lists_path
    end

    private

    def list_params
        params.require(:list).permit(:title)
    end

end
和Index.html代码(如果需要)

<div class="col-md-12">
    <h1>ToDo List</h1>

    <div class="buffer-top"><%= link_to "New List", new_list_path, class: 'btn btn-primary' %></div>

    <div class="buffer-top">
        <% @lists.each do |list| %>
            <%= render partial: 'list', object: list %>
        <% end %>
    </div>

</div>

您没有对索引视图的用户进行身份验证,错误是因为没有当前用户,因此您正在调用nil上的方法列表

您应该首先检查是否有用户

def index
  if user_signed_in?
   @lists = current_user.lists.order('created_at')
  else
    <!-- Do somthing here, what do you want if no user is there? -->
  end
end
如果您真的想使用这个设置,那么您可以通过删除index方法中的行并更改视图来使其变干

<% if user_signed_in? %>
  <div class="buffer-top">
    <% current_user.lists.order('created_at').each do |list| %>
        <%= render partial: 'list', object: list %>
    <% end %>
  </div>
<% end %>
<% if user_signed_in? %>
  <div class="buffer-top">
    <% @lists.each do |list| %>
        <%= render partial: 'list', object: list %>
    <% end %>
  </div>
<% end %>
<% if user_signed_in? %>
  <div class="buffer-top">
    <% current_user.lists.order('created_at').each do |list| %>
        <%= render partial: 'list', object: list %>
    <% end %>
  </div>
<% end %>