【问题标题】:Undefined method for `before_filter'`before_filter' 的未定义方法
【发布时间】:2017-07-10 15:11:00
【问题描述】:

尽管在下一行中明确定义了该方法,但我遇到了以下错误。

undefined method `before_filter' for AuthorsController:Class

我正在关注这个tutorial

代码sn-p如下:

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

使用 Sorcery 作为身份验证,Windows 10,可视代码。

【问题讨论】:

  • 您使用的是哪个版本的 Rails?
  • Rails 5.1.2,谢谢我不知道 before_filter 已被弃用。
  • before_actionbefore_filter 之间没有区别,只是 before_filter 已被弃用或删除。请改用before_action

标签: ruby-on-rails-5.1


【解决方案1】:

尝试使用

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

代替

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

before_filter 在 Rails 5.0 中已被弃用并在 5.1 中被删除。

【讨论】:

【解决方案2】:

如果您在 Rails 版本

class AuthorsController < ApplicationController
end

【讨论】:

    【解决方案3】:

    milia on rails 5.2 版的设置

    宝石文件:

    • gem '设计'
    • 宝石'milia'

    应用程序控制器:

    class ApplicationController < ActionController::Base
      before_action :authenticate_tenant! 
    end
    

    devise_permitted_pa​​rameter.rb:

      included do
        before_filter :configure_permitted_parameters
      end
    

    上面改为

    included do
        before_action :configure_permitted_parameters
      end
    

    运行命令: rails g milia:install --org_email='do-not-reply@local-test.dev'

    重要提示:当它要求覆盖 devise_permitted_pa​​rameter.rb 文件

    【讨论】:

      猜你喜欢
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 2013-06-11
      相关资源
      最近更新 更多