2015-04-03 75 views
2

我一直在搜索遍地,无法找到任何地方的答案。NoMethodError Rails 3.2

我正在参加RoR课程的udemy课程介绍,我已经能够解决我在课程的前80%所遇到的所有问题,但现在我在路障中,可以找不到这个。我们正在构建一个类似Etsy的应用程序,而且我正需要限制用户编辑/删除不属于他们的列表。

我on Rails的Ruby的运行3.2.21 1.9.3

我尝试以下,用于将检查用户过滤器的指令,但是当我检查回本地主机上,我收到此错误:

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

如果需要任何其他信息,以帮助回答这个问题,请让我知道。

回答

0

这不是Rails 3 vs 4问题,你的代码永远不会调用set_listing,所以@listing永远不会被设置。你或许应该有一个:

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

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

+1

太谢谢你了!我一直在努力解决这个问题。由于某种原因,教师的代码自动调用'set_listing',所以我不知道我需要将其输入到我的代码中。 只要StackOverflow允许,我会在几分钟内将您的答案标记为已接受。 – 2015-04-03 19:18:27

+0

不客气。 – smathy 2015-04-03 19:25:34