未定义的方法`any?' for nil:NilClass for<%if @ users.any? %GT;

时间:2016-05-06 18:44:40

标签: ruby ruby-on-rails-4 railstutorial.org

我正在关注Micheal Hartl Ruby on Rails教程。在第12章中,我收到了这个错误。

我正在尝试显示以下内容和关注者,但我无法做到。

This is the error I am getting.

以下是代码:

  

show.html.erb

<% provide(:title, @title) %>
<div class="row">
  <aside class="col-md-4">
    <section class="user_info">
      <%= gravatar_for @user %>
      <h1><%= @user.name %></h1>
      <span><%= link_to "view my profile", @user %></span>
      <span><b>Microposts:</b> <%= @user.microposts.count %></span>
    </section>
    <section class="stats">
      <%= render 'shared/stats' %>
      <% if @users.any? %>
        <div class="user_avatars">
          <% @users.each do |user| %>
            <%= link_to gravatar_for(user, size: 30), user %>
          <% end %>
        </div>
      <% end %>
    </section>
  </aside>
  <div class="col-md-8">
    <h3><%= @title %></h3>
    <% if @users.any? %>
      <ul class="users follow">
        <%= render @users %>
      </ul>
      <%= will_paginate %>
    <% end %>
  </div>
</div>
  

relationship_controller.rb

class RelationshipsController < ApplicationController
  before_action :logged_in_user

  def create
 def create
    @user = User.find(params[:followed_id])
    current_user.follow(user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end

  def destroy
    @user = Relationship.find(params[:id]).followed
    current_user.unfollow(user)
   respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end
end
  

user_controller.rb

  class UsersController < ApplicationController
 before_action :logged_in_user, only: [:index, :edit, :update, :destroy,
                                        :following, :followers]
  before_action:correct_user,   only: [:edit, :update]
  before_action:admin_user,     only: :destroy

    def index
  @users= User.paginate(page: params[:page])
    end


    def show
  @user= User.find(params[:id])
  @microposts = @user.microposts.paginate(page: params[:page], :per_page => 5)

    end

    def new
  @user= User.new
    end

    def create
  @user= User.new(user_params)
      if @user.save
       @user.send_activation_email
  flash[:info] = "Please check your email to activate your account."
        redirect_to root_url
      else
        render 'new'
      end
    end

     def edit
  @user= User.find(params[:id])
     end

     def update
  @user= User.find(params[:id])
      if @user.update_attributes(user_params)
  flash[:success] = "Profile updated"
        redirect_to @user
      else
        render 'edit'
      end
     end

     def destroy
  User.find(params[:id]).destroy
  flash[:success] = "User deleted"
      redirect_to users_url
     end

    def following
    @title = "Following"
    @user  = User.find(params[:id])
    @users = @user.following.paginate(page: params[:page])
    render 'show_follow'
  end

  def followers
    @title = "Followers"
    @user  = User.find(params[:id])
    @users = @user.followers.paginate(page: params[:page])
    render 'show_follow'
  end


    private

      def user_params
  params.require(:user).permit(:name, :email, :password,
                                     :password_confirmation)
      end




      def correct_user
  @user= User.find(params[:id])
  redirect_to(root_url) unless @user== current_user
      end


      def admin_user
        redirect_to(root_url) unless current_user.admin?
      end

  end

2 个答案:

答案 0 :(得分:2)

您的问题是show操作实际上是使用@user而不是@users实例变量。

您的统计信息部分不起作用,因为它预期User的集合看起来像index行动一样。

由于followers的操作设置了@user@users个实例变量,您必须访问该操作,而show.html.erb模板上的代码最多可能必须转移到show_follow.html.erb模板。

<强>更新

您需要从show.html.erb模板中删除此部分

<section class="stats">
      <%= render 'shared/stats' %>
      <% if @users.any? %>
        <div class="user_avatars">
          <% @users.each do |user| %>
            <%= link_to gravatar_for(user, size: 30), user %>
          <% end %>
        </div>
      <% end %>
    </section>
  </aside>
  <div class="col-md-8">
    <h3><%= @title %></h3>
    <% if @users.any? %>
      <ul class="users follow">
        <%= render @users %>
      </ul>
      <%= will_paginate %>
    <% end %>
  </div>

答案 1 :(得分:1)

我看到了你的存储库,看起来你已经弄乱了代码,根据你所关注的内容(Ruby on Rails - Micheal Hartl),这就是你需要改变的地方:

show.html.erb中的代码应放在show_follow.html.erb

enter image description here

之后再次按照第12章进行操作,并将正确的代码放在show.html.erb中。