用户控制器中没有方法错误//未定义方法`any?'为零:NilClass

时间:2015-03-23 17:29:39

标签: ruby-on-rails ruby

我正在尝试让我的网站上的用户能够查看他们上传的其他用户的汽车。在用户查看自己的汽车的主页上,我通过渲染保存用户汽车的shared/feed来完成此操作。这样做不会给我带来任何错误,但是当我渲染shared/feed以在另一个用户的页面上显示汽车时,我会在undefined method 'any?' for nil:NilClass

的第二行显示shared/feed

共享/进料

<div id="car_feed">
    <% if @feed_items.any? %>
      <ul class="cars">
        <%= render @feed_items %>
      </ul>
    <% else %>
      <h3> You haven't added any cars yet.</h3>
      <h3> Add one in the sidebar!</h3>
    <% end %>
</div>

在主页上,这样可以正常工作。

home.html.erb

<% if logged_in? %>
<div>
  <aside class="col-md-4">
    <div>
      <h1>My Profile</h1>
    </div>
    <div class="hero-unit user_info">
      <%= render 'shared/user_info' %>
    </div>
    <section class="hero-unit car_form">
      <%= render 'shared/car_form' %>
    </section>
  </aside>

  <div class="col-md-8">
    <h1>My Cars</h1>
  </div>
  <div class="col-md-8">
    <%= render 'shared/feed' %>
  </div>
</div>

<% else %>
//landing page content
<% end %>

但是在渲染展示页面时出现错误

show.html.erb

<div>
  <%= render 'users/user' %>
</div>

_user.html.erb

<div>
  <aside class="col-md-4">
    <div>
      <h1>My Profile</h1>
    </div>
    <div class="hero-unit user_info">
      <h1><%= @user.name %></h1>
      <h2><%= pluralize(@user.cars.count, "Car") %></h2>
    </div>
  </aside>

  <div class="col-md-8">
    <h1>My Cars</h1>
  </div>
  <div class="col-md-8">
    <%= render 'shared/feed' %>
  </div>
</div>

cars_controller

class CarsController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy, :edit]
  before_action :correct_user, only: [:destroy, :edit]

  def create
      @car = current_user.cars.build(cars_params)
      respond_to do |format|
          if @car.save
              @feed_items = current_user.feed
              format.html { redirect_to root_url }
              format.js
          else
              format.html { render 'static_pages/home' }
              format.js
          end
      end
  end

  def destroy
    @car.destroy
    flash[:success] = "Car deleted"
    redirect_to request.referrer || root_url
  end

  def edit
    @car.edit
    flash[:success] = "Car edited"
    redirect_to request.referrer || root_url
  end

  private

    def cars_params
      params.require(:car).permit(:year, :brand, :model, :vin, :mileage, :picture)
    end

    def correct_user
      @car = current_user.cars.find_by(id: params[:id])
      redirect_to root_url if @car.nil?
    end
end

users_controller

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

    def index
      @users = User.all
    end

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

    def new
      @user = User.new
    end

    def create
      @user = User.new(user_params)
      if @user.save
        log_in @user
        flash[:success] = "Thank you for signing up, welcome to ensage!"
        redirect_to root_path
      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

    private

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

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

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

staticpages_controller

class StaticPagesController < ApplicationController

  def home
    if logged_in?
      @car = current_user.cars.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end

  def shop_home
    if shop_logged_in?
      @shop = current_shop
    end
  end

  def help
  end

  def about 
  end

  def contact
  end
end

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

在没有看到您的控制器的情况下,我无法确认您是否已将#feed_items变量声明为#show控制器,就好像您的#index方法一样。

如果你已经把它排除在外,那么@feed_items就不存在了,这就是为什么要打电话.any?在一个零变量上没有工作。

<强> 更新 查看最后一个静态控制器:

def home
    if logged_in?
      @car = current_user.cars.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end

为什么它适用于家庭。用户/节目控制器需要类似:

  def show
    #don't forget to put whatever other pertinent code goes in this controller
    if logged_in?
      @feed_items = a_user_you_selected.feed.paginate(page: params[:page])
    end
  end

希望有所帮助。

答案 1 :(得分:0)

我猜您的@feed_items集合为nil,而#any?上的nil则会提升NoMethodError

如果只有一个有效元素,any?将返回true,如果所有元素都为nil,则返回false,如果对象本身为nil则引发异常

[nil, "car", nil].any?
#=> true

[nil, nil, nil].any?
#=> false

[].any?
#=> false

nil.any?
#=> NoMethodError:

您在rending @feed_items部分时需要将user作为当地人传递:

<%= render 'users/user', locals: {feed_items: @feed_items} %>
                                              ^ instance variable declared in show action

然后在您的用户部分中,您要呈现feed部分,您需要再次传递feed_items

<%= render 'shared/feed', locals: {feed_items: feed_items} %>
                                               ^ not the instance variable

您的Feed将如下所示:

<div id="car_feed">
  <% if feed_items %>
    <ul class="cars">
      <%= render feed_items %>
    </ul>
  <% else %>
    <h3> You haven't added any cars yet.</h3>
    <h3> Add one in the sidebar!</h3>
  <% end %>
</div>

如果您想查看该特定用户的Feed项,那么您在用户控制器中的展示操作必须如此:

def show
  @user = User.find(params[:id])
  @feed_items = @user.feed.paginate(page: params[:page])
end