NoMethodError Rails 3.2

NoMethodError Rails 3.2

我一直在搜索,但找不到任何地方的答案。

我正在 udemy 上介绍 RoR 课程,我已经能够解决我在课程前 80% 中遇到的所有问题,但现在我遇到了障碍,并且找不到这个。我们正在构建一个像 Etsy 这样的应用程序,我正处于需要限制用户访问 editing/deleting 不属于他们的列表的地步。

我 运行 Ruby 1.9.3 Rails 3.2.21

我尝试按照说明添加检查用户过滤器,但当我在本地主机上重新检查时,我收到此错误:

NoMethodError in ListingsController#edit

undefined method `user' for nil:NilClass

app/controllers/listings_controller.rb:98:in `check_user'

Parameters: {"id"=>"8"}

我的代码和导师的代码完全一致,但我认为这个错误是因为我用的是Rails 3,而他用的是4。

这是我的 listings_controller.rb

class ListingsController < ApplicationController
  # GET /listings
  # GET /listings.json
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
  before_filter :check_user, only: [:edit, :update, :destroy]

  def index
    @listings = Listing.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @listings }
    end
  end

  # GET /listings/1
  # GET /listings/1.json
  def show
    @listing = Listing.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @listing }
    end
  end

  # GET /listings/new
  # GET /listings/new.json
  def new
    @listing = Listing.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @listing }
    end
  end

  # GET /listings/1/edit
  def edit
    @listing = Listing.find(params[:id])
  end

  # POST /listings
  # POST /listings.json
  def create
    @listing = Listing.new(params[:listing])
    @listing.user_id = current_user.id

    respond_to do |format|
      if @listing.save
        format.html { redirect_to @listing, notice: 'Listing was successfully created.' }
        format.json { render json: @listing, status: :created, location: @listing }
      else
        format.html { render action: "new" }
        format.json { render json: @listing.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /listings/1
  # PUT /listings/1.json
  def update
    @listing = Listing.find(params[:id])

    respond_to do |format|
      if @listing.update_attributes(params[:listing])
        format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @listing.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /listings/1
  # DELETE /listings/1.json
  def destroy
    @listing = Listing.find(params[:id])
    @listing.destroy

    respond_to do |format|
      format.html { redirect_to listings_url }
      format.json { head :no_content }
    end
  end

  private
  def set_listing
    @listing = Listing.find(params[:id])
  end

  def listing_params
    params.require(:listing).permit(:name, :description, :price, :image)
  end

  def check_user
    if current_user != @listing.user
      redirect_to root_url, alert: "Sorry, this listing belongs to someone else."
    end
  end

end

我们必须为此添加的代码是第二个 before_filter 和 def check_user

如果需要任何其他信息来帮助回答这个问题,请告诉我。

这不是 Rails 3 对 4 问题,您的代码永远不会调用 set_listing,因此永远不会设置 @listing。你可能应该有一个:

before_filter :set_listing, only: [:show, :edit, :update, :destroy]

在文件的顶部,before_filter :check_user, ...

之前