Ruby on rails Rails 4与权威人士和;政治家宝石-当一个物体处于一种状态时的政策

Ruby on rails Rails 4与权威人士和;政治家宝石-当一个物体处于一种状态时的政策,ruby-on-rails,state-machine,pundit,Ruby On Rails,State Machine,Pundit,我正在尝试在Rails 4中制作一个应用程序 我试图用政治家的智慧来为各州服务,然后用权威来为政策服务 我的文件有: gem 'statesman', '~> 1.3', '>= 1.3.1' gem 'pundit' 我有一个文章模型、一个文章转换模型和一个文章状态机模型 我的目标是在我的文章策略中定义一个发布策略(使用pundit),允许拥有文章的用户在文章处于“已批准”状态时发布该文章 我在我的权威文章政策中尝试了这一点: class ArticlePolicy < A

我正在尝试在Rails 4中制作一个应用程序

我试图用政治家的智慧来为各州服务,然后用权威来为政策服务

我的文件有:

gem 'statesman', '~> 1.3', '>= 1.3.1'
gem 'pundit'
我有一个文章模型、一个文章转换模型和一个文章状态机模型

我的目标是在我的文章策略中定义一个发布策略(使用pundit),允许拥有文章的用户在文章处于“已批准”状态时发布该文章

我在我的权威文章政策中尝试了这一点:

class ArticlePolicy < ApplicationPolicy

def publish?

user.present? && user == article.user 
# if requires approval, then approved

# and article.in_state(:approve) - why doesnt this work - see statesman docs? 

 # user && user.article.exists?(article.id)

 end
end
第条过渡模式:

class ArticleTransition < ActiveRecord::Base
  include Statesman::Adapters::ActiveRecordTransition


  belongs_to :article, inverse_of: :article_transitions



end
有人能看出我做错了什么吗

整个项目控制器具有:

class ArticlesController < ApplicationController
  before_action :set_article, only: [:show, :edit, :update, :destroy, :reject, :approve, :publish, :remove]
  before_action :authenticate_user!, except: [:index, :show, :search, :reject, :approve, :publish, :remove]


  respond_to :html, :json
# GET /articles
  # GET /articles.json
  def index
    @articles = policy_scope(Article)
    # query = params[:query].presence || "*"
    # @articles = Article.search(query)
  end

  # def index
  #   if params[:query].present?
  #     @books = Book.search(params[:query], page: params[:page])
  #   else
  #     @books = Book.all.page params[:page]
  #   end
  # end

  # GET /articles/1
  # GET /articles/1.json
  def show

  end

  # GET /articles/new
  def new
    @article = Article.new
    @article.comments.build
  end

  # GET /articles/1/edit
  def edit

    authorize @article
  end

  # POST /articles
  # POST /articles.json
  def create
    # before_action :authenticate_user!
    # authorize @article
    @article = current_user.articles.new(article_params)

    respond_to do |format|
      if @article.save
        format.html { redirect_to(@article) }
        format.json { render :show, status: :created, location: @article }
      else
        format.html { render :new }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  def search
    if params[:search].present?
      @articless = Article.search(params[:search])
    else 
      @articles = Articles.all
    end
  end


  # PATCH/PUT /articles/1
  # PATCH/PUT /articles/1.json
  def update
    # before_action :authenticate_user!
    authorize @article
    respond_to do |format|
    #   if @article.update(article_params)
    #     format.json { render :show, status: :ok, location: @article }
    #   else
    #     format.html { render :edit }
    #     format.json { render json: @article.errors, status: :unprocessable_entity }
    #   end
    # end
      if @article.update(article_params)
         format.html { redirect_to(@article) }
        format.json { render :show, status: :ok, location: @article }
      else
        format.json { render json: @article.errors, status:      :unprocessable_entity }
      end
      format.html { render :edit }
    end
  end



  # DELETE /articles/1
  # DELETE /articles/1.json
  def destroy
    before_action :authenticate_user!
    authorize @article
    @article.destroy
    respond_to do |format|
      format.json { head :no_content }
    end
  end

  # def review
  #   article = Article.find(params[:id])
  #   if article.state_machine.transition_to!(:review)
  #     flash[:notice] = "Comments on this article have been made for your review"
  #     redirect_to action: :show, id: article_id
  #   else
  #     flash[:error] = "You're not able to review this article"
  #     redirect_to action: :show, id: article_id
  #   end
  # end

  def reject
  end

  def approve
    article = Article.find(params[:id])
    if article.state_machine.transition_to!(:approve)
      flash[:notice] = "This article has been approved for publication"
      redirect_to action: :show, id: article_id
      # add mailer to send message to article owner that article has been approved
    else
      flash[:error] = "You're not able to approve this article"
      redirect_to action: :show, id: article_id
    end
  end

  def publish
    article = Article.find(params[:id])
    if article.state_machine.transition_to!(:publish)
      redirect_to action: :show, id: article_id
      # how do you catch the date the state became published?
    else
      flash[:error] = "You're not able to publish this article"
      redirect_to action: :show, id: article_id
    end
  end

  def remove
    article = Article.find(params[:id])
    if article.state_machine.transition_to!(:remove)
      redirect_to root_path
    else
      flash[:error] = "You're not able to destroy this article"
      redirect_to action: :show, id: article_id
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_article
      @article = Article.find(params[:id])
      authorize @article
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def article_params
      params.require(:article).permit(:body, :title, :image, :tag_list,
        comment_attributes: [:opinion])
    end

end
class-ArticlesController
当我试图检查文章是否处于状态时:approve(如注释所示 我收到一条错误消息,上面说未定义的方法 “处于”状态


您是否尝试将策略中的article.in_state(:approve)更改为article.state_machine.in_state(:approve)

您错过了方法末尾的

该方法实际上是一个类方法,其行为类似于作用域

您需要使用方法,它是一个实例方法,如下所示:


article.state\u machine.in\u state?(:approve)

您正在使用的statesman gem版本未定义
in\u state?
。你可以更新gem。或者,您可以使用smallbuttoncom链接的类似代码自己定义它

然而,对于您的情况,一个简单的检查就足够了。请在策略中尝试以下代码

article.state_machine.current_state == "approve"
希望能有帮助。

你在哪里
  def approve
    article = Article.find(params[:id])
    if article.state_machine.transition_to!(:approve)
      flash[:notice] = "This article has been approved for publication"
      redirect_to action: :show, id: article_id
      # add mailer to send message to article owner that article has been approved
    else
      flash[:error] = "You're not able to approve this article"
      redirect_to action: :show, id: article_id
    end
  end

def publish
    article = Article.find(params[:id])
    authorize @article

    if article.state_machine.transition_to!(:publish)
      redirect_to action: :show, id: article_id
      # how do you catch the date the state became published?
    else
      flash[:error] = "You're not able to publish this article"
      redirect_to action: :show, id: article_id
    end
  end
class ArticlesController < ApplicationController
  before_action :set_article, only: [:show, :edit, :update, :destroy, :reject, :approve, :publish, :remove]
  before_action :authenticate_user!, except: [:index, :show, :search, :reject, :approve, :publish, :remove]


  respond_to :html, :json
# GET /articles
  # GET /articles.json
  def index
    @articles = policy_scope(Article)
    # query = params[:query].presence || "*"
    # @articles = Article.search(query)
  end

  # def index
  #   if params[:query].present?
  #     @books = Book.search(params[:query], page: params[:page])
  #   else
  #     @books = Book.all.page params[:page]
  #   end
  # end

  # GET /articles/1
  # GET /articles/1.json
  def show

  end

  # GET /articles/new
  def new
    @article = Article.new
    @article.comments.build
  end

  # GET /articles/1/edit
  def edit

    authorize @article
  end

  # POST /articles
  # POST /articles.json
  def create
    # before_action :authenticate_user!
    # authorize @article
    @article = current_user.articles.new(article_params)

    respond_to do |format|
      if @article.save
        format.html { redirect_to(@article) }
        format.json { render :show, status: :created, location: @article }
      else
        format.html { render :new }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  def search
    if params[:search].present?
      @articless = Article.search(params[:search])
    else 
      @articles = Articles.all
    end
  end


  # PATCH/PUT /articles/1
  # PATCH/PUT /articles/1.json
  def update
    # before_action :authenticate_user!
    authorize @article
    respond_to do |format|
    #   if @article.update(article_params)
    #     format.json { render :show, status: :ok, location: @article }
    #   else
    #     format.html { render :edit }
    #     format.json { render json: @article.errors, status: :unprocessable_entity }
    #   end
    # end
      if @article.update(article_params)
         format.html { redirect_to(@article) }
        format.json { render :show, status: :ok, location: @article }
      else
        format.json { render json: @article.errors, status:      :unprocessable_entity }
      end
      format.html { render :edit }
    end
  end



  # DELETE /articles/1
  # DELETE /articles/1.json
  def destroy
    before_action :authenticate_user!
    authorize @article
    @article.destroy
    respond_to do |format|
      format.json { head :no_content }
    end
  end

  # def review
  #   article = Article.find(params[:id])
  #   if article.state_machine.transition_to!(:review)
  #     flash[:notice] = "Comments on this article have been made for your review"
  #     redirect_to action: :show, id: article_id
  #   else
  #     flash[:error] = "You're not able to review this article"
  #     redirect_to action: :show, id: article_id
  #   end
  # end

  def reject
  end

  def approve
    article = Article.find(params[:id])
    if article.state_machine.transition_to!(:approve)
      flash[:notice] = "This article has been approved for publication"
      redirect_to action: :show, id: article_id
      # add mailer to send message to article owner that article has been approved
    else
      flash[:error] = "You're not able to approve this article"
      redirect_to action: :show, id: article_id
    end
  end

  def publish
    article = Article.find(params[:id])
    if article.state_machine.transition_to!(:publish)
      redirect_to action: :show, id: article_id
      # how do you catch the date the state became published?
    else
      flash[:error] = "You're not able to publish this article"
      redirect_to action: :show, id: article_id
    end
  end

  def remove
    article = Article.find(params[:id])
    if article.state_machine.transition_to!(:remove)
      redirect_to root_path
    else
      flash[:error] = "You're not able to destroy this article"
      redirect_to action: :show, id: article_id
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_article
      @article = Article.find(params[:id])
      authorize @article
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def article_params
      params.require(:article).permit(:body, :title, :image, :tag_list,
        comment_attributes: [:opinion])
    end

end
article.state_machine.current_state == "approve"