簡體   English   中英

RoR-方法“管理員?” 對於nil:NilClass

[英]RoR - method 'admin?' for nil:NilClass

我只是想重新提出這個問題 我在類似的控制器上遇到了同樣的問題:

    class CategoriesController < ApplicationController

  before_action :require_admin, except: [:index, :show]

  def index
    @categories = Category.paginate(page: params[:page], per_page: 5)
  end

  def new
    @category = Category.new
  end

  def create
    @category = Category.new(category_params)
    if @category.save
      flash[:success] = "La nueva categoria se creó correctamente"
      redirect_to categories_path
    else 
      render 'new'
    end
  end

  def index
     @categories = Category.paginate(page: params[:page], per_page: 5)
  end

  def show
  end

  private

  def category_params
    params.require(:category).permit(:name)
  end

  def require_admin
    if !logged_in? || logged_in? and !current_user.admin?
      flash[:danger] = "Solamente el admin puede acceder a esta seccion"
    redirect_to categories_path
    end
  end


end

並由於某種原因產生了錯誤的未定義方法“ admin”? for nil:NilClass當我對另一部分使用完全相同的代碼時,它工作得很好:

    Class ArticlesController < ApplicationController

  before_action :set_article, only: [:edit, :update, :show, :destroy]
  before_action :require_user, only: [:new, :create, :update, :edit, :destroy]
  before_action :require_same_user, only: [:update, :edit, :destroy]

  def index
    @articles = Article.paginate(page: params[:page], per_page: 5)
  end

  def new
    @article = Article.new
  end

  def create
    @article = Article.new(article_params)
    @article.user = current_user
    if @article.save
      flash[:success] = "El articulo fue correctamente creado!"
      redirect_to article_path(@article)
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @article.update(article_params)
      flash[:success] = "El articulo fue correctamente actualizado!"
      redirect_to article_path(@article)
    else
      render 'edit'
    end
  end

  def show
  end

  def destroy
    @article.destroy
    flash[:success] = "El articulo fue correctamente borrado!"
    redirect_to articles_path
  end

  private
    def set_article
      @article = Article.find(params[:id])
    end

    def article_params
      params.require(:article).permit(:title, :description)
    end

    def require_same_user
      if current_user != @article.user and !current_user.admin?
        flash[:danger] = "Solamente puede editar y borrar sus propios articulos"
        redirect_to root_path
      end
    end

無論如何,我已經切換到Drew的解決方案,並且現在可以使用,但是我想知道首先是什么問題? 為什么它在我的代碼的另一部分中起作用,而在這個特定的代碼中卻不起作用?

提前致謝!

如果!logged_in? || 登錄? 和!current_user.admin?

它將評估!logged_in? || logged_in? !logged_in? || logged_in? 首先(始終為true),然后是!current_user.admin? ,因此它將始終檢查!current_user.admin? ,但是當用戶未登錄時current_user將為nil ...

我想您是否要if !logged_in? || (logged_in? and !current_user.admin?) if !logged_in? || (logged_in? and !current_user.admin?)

但是您可以用unless而不是if重寫它(我認為可讀性更好)?

除非current_user && current_user.admin?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM