2017-07-10 23 views
7

Występuje następujący błąd, mimo że metoda jest wyraźnie zdefiniowana w następnym wierszu.Niezdefiniowana metoda dla `before_filter '

undefined method `before_filter' for AuthorsController:Class 

Postępuję zgodnie z tym tutorial.

Fragment kodu następująco:

authors_controller.rb

class AuthorsController < ApplicationController 

    before_action :set_author, only: [:show, :edit, :update, :destroy] 
    before_filter :zero_authors_or_authenticated, only: [:new, :create] 

    def zero_authors_or_authenticated 
    # If either one of them is true this filter won’t do anything, allowing the requested user registration form to be rendered 
    unless Author.count == 0 || current_user # checking if there are either zero registered users OR if there is a user already logged in 
     redirect_to root_path # if neither are true, then redirect to root_path and return false 
     return false 
    end 
    end 

    # GET /authors 
    # GET /authors.json 
    def index 
    @authors = Author.all 
    end 

    # GET /authors/1 
    # GET /authors/1.json 
    def show 
    end 

    # GET /authors/new 
    def new 
    @author = Author.new 
    end 

    # GET /authors/1/edit 
    def edit 
    end 

    # POST /authors 
    # POST /authors.json 
    def create 
    @author = Author.new(author_params) 

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

    # PATCH/PUT /authors/1 
    # PATCH/PUT /authors/1.json 
    def update 
    respond_to do |format| 
     if @author.update(author_params) 
     format.html { redirect_to @author, notice: 'Author was successfully updated.' } 
     format.json { render :show, status: :ok, location: @author } 
     else 
     format.html { render :edit } 
     format.json { render json: @author.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /authors/1 
    # DELETE /authors/1.json 
    def destroy 
    @author.destroy 
    respond_to do |format| 
     format.html { redirect_to authors_url, notice: 'Author was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_author 
     @author = Author.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def author_params 
     params.require(:author).permit(:username, :email, :password, :password_confirmation) 
    end 
end 

Korzystanie Sorcery jak uwierzytelnianie, Windows 10, kod wizualny.

+1

Która wersja Rails używasz? – 31piy

+0

Rails 5.1.2, dziękuję, nie wiedziałem, że before_filter jest przestarzałe. – Carrein

+0

Nie ma różnicy między 'before_action' i' before_filter' z tym wyjątkiem, że 'before_filter' jest przestarzałe lub usunięte. Zamiast tego użyj 'before_action'. – SteveTurczyn

Odpowiedz

15

Spróbuj użyć

before_action :zero_authors_or_authenticated, only: [:new, :create] 

Zamiast

before_filter :zero_authors_or_authenticated, only: [:new, :create] 

before_filter została zaniechana w Rails 5.0 i usunięte w 5.1.

1

Jeśli otrzymujesz ten błąd w szynach 4.2.7, należy sprawdzić, czy zostały odziedziczone kontrolera z kontrolera akcji

class AuthorsController < ApplicationController 
end