Rails 4 - Pundit - 索引范围策略

2023-12-04

我正在尝试学习如何将 Pundit 与我的 Rails 4 应用程序一起使用。

我有以下型号:

class User < ActiveRecord::Base
  has_one :profile
  has_many :eois
end

class Profile < ActiveRecord::Base
  belongs_to :user
  has_many :projects, dependent: :destroy
end

class Project < ActiveRecord::Base
  belongs_to :profile
  has_many :eois
end

class Eoi < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
end

我有一个范围EoiPolicy with:

class EoiPolicy < ApplicationPolicy

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user  = user
      @scope = scope
    end

    def resolve
      if user.profile.project.id == @eoi.project_id?
        scope.where(project_id: @user.profile.project.id)
      elsif user.id == eoi.user_id?
        scope.where(user_id: user.id)
      else
        nil
      end
    end
  end

  def index?
    user.profile.project.id == @eoi.project_id? or user.id == eoi.user_id?
  end

  def new?
    true
  end

  def show?
    user.profile.project.id == @eoi.project_id? or user.id == eoi.user_id?
  end

  def edit?
    user.id == eoi.user.id?
  end

  def create?
    true 
  end

  def update?
    user.id == eoi.user.id?
  end

  def destroy?
    user.id == eoi.user.id?
  end    
end

In my EoisController,我尝试使用范围:

def index
  # @eois = @project.eois
  @eois = policy_scope(Eoi)
  # @eois = Eois.find_by_project_id(params[:project_id])
end

然后在我的view/eois/index,我尝试用以下方式显示索引:

<% policy_scope(@user.eois).each do |group| %>

我无法让它发挥作用。错误消息突出显示了策略中我的作用域方法的这一行:

if user.profile.project.id == @eoi.project_id?

对我来说,这看起来是正确的,尽管我仍在努力弄清楚这一点。任何人都可以看到需要发生什么才能使这项工作正常进行,这样,如果用户是用户,其个人资料拥有相关项目,则与该项目相关的所有 eois 都是可见的。

否则,如果用户是创建eoi的用户,那么他们创建的所有eoi都是可见的?

错误消息显示:

undefined method `project' for #<Profile:0x007fa03f3faf48>
Did you mean?  projects
               projects=

我想知道这是否是因为索引会有很多记录,它需要在策略中显示不同的内容才能识别复数?

我也尝试用以下内容替换该行:

if  @eoi.project_id == @user.profile.project.id?

尽管这也是错误的并且给出

undefined method `project_id' for nil:NilClass
Did you mean?  object_id

我也尝试制定范围:

 def resolve
      # cant figure what is wrong with this
      if  eoi.project_id == user.profile.project.id?
        scope.where(project_id: @user.profile.project.id)
      else
        nil
      end
    end

但这也是错误的,并给出了这个错误:

  undefined local variable or method `eoi' for #<EoiPolicy::Scope:0x007ffb505784f8>

我也尝试过:

    def resolve
      # cant figure what is wrong with this

      if  @eoi.project_id == user.profile.project.id? or Eoi.project_id == user.profile.project.id?
        scope.where(project_id: @user.profile.project.id)
      elsif user.id == eoi.user_id?
        scope.where(user_id: user.id)
      else
        nil
      end
    end
  end



def index?
    user.profile.project.id == Eoi.project_id? or user.id == Eoi.user_id?
  end

但该尝试给出了此错误消息:

undefined method `project_id' for nil:NilClass
Did you mean?  object_id

目前的想法

我认为我需要将用户和范围传递给范围方法。如果我也可以通过项目,那么我可以使范围引用 EoI 相关的项目。

如果我可以让它工作,那么也许我可以让范围方法适用于控制器上的索引视图:

class Scope
  attr_reader :user, :scope

  def initialize(user, scope, project)
    @user  = user
    @scope = scope
    @project = project
  end
end

然后在控制器中:

 def index
   # @eois = @project.eois

   @eois = policy_scope(Eoi, @project)
   # authorize @eois
   # @eois = Eois.find_by_project_id(params[:project_id])
 end

这不起作用,当我尝试时,我收到一条错误消息,指出该政策

wrong number of arguments (given 2, expected 1)

请帮忙!

下一次尝试

我的下一个尝试是尝试采纳 [this]Pundit 问题中的建议,并实现如何为特定用户获取正确范围的想法。

在我的 Eoi 策略中,我将解析方法更改为:

class Scope
    attr_reader :user, :scope

    def initialize(user, scope) #project
      @user  = user
      @scope = scope
      # @project = project

    end

    def resolve
      # if  Eoi.project_id == user.profile.project.id? or Eoi.project_id == user.profile.project.id?
      if user.id == eoi.projects.profile.user.map(&:id)
        scope.joins(eois: :projects).where(project_id: user.profile.projects.map(&:id)).empty?
      # if scope.eoi.project_id == user.profile.projects.map(&:id)  
        # scope.where(project_id: user.profile.projects.map(&:id)).empty? 
      #   scope.where(project_id: user.profile.project.id)
      # elsif user.id == eoi.user_id?
      #   scope.where(user_id: user.id)
      else
      #   nil
       end
    end
  end

然后在我的 eoi 控制器索引操作中,我尝试了以下操作:

def index
    # @eois = @project.eois

    # @eois = policy_scope(Eoi, @project)
    policy_scope(Eoi).where(project_id: params[:project_id])
    # authorize @eois
    # @eois = Eois.find_by_project_id(params[:project_id])
  end

那也行不通。此尝试的错误消息显示:

undefined local variable or method `eoi' for #<EoiPolicy::Scope:0x007f98677c9cf8>

我没有尝试的想法。任何人都可以找到一种方法来为示波器提供正确的输入来进行设置吗?

观察我注意到 github 上很多使用 Pundit 和范围的存储库也包含这样的方法:

def scope
  Pundit.policy_scope!(user, record.class)
end

该方法是 Scope 类的补充,并且未在 Pundit gem 文档中显示。如果有必要包含它,它有什么作用?1

REWRITE

我现在已经浏览了 github 上的 200 多个存储库,以深入了解应该如何编写策略来实现我的目标。我不知道如何按预期使用 Pundit。

我已经完全改变了我的设置,尝试解决我无法理解的问题。我现在有:

Eois控制器

class EoisController < ApplicationController

  def index
    @eois = Eoi.by_user_id(current_user.id)
  end
end

项目:: Eois 控制器

module Projects
  class EoisController < ApplicationController
    before_action :get_project
    before_action :set_eoi, only: [:edit, :update, :destroy]
    # after_action :verify_authorized

    def index
      @eois = Project.by_user_id(current_user.id).find_by(id: params[:project_id]).try(:eois) || []
    end

 def show
      @eoi = Eoi.find(params[:id])
      authorize @eoi
    end

def set_eoi
        @eoi = EoiPolicy::Scope.new(current_user, params[:project_id]).resolve.find(params[:id])
      end

      def get_project
        @project = Project.find(params[:project_id])
      end

Eoi 策略(决定何时显示用户创建的所有 eoi)

class EoiPolicy < ApplicationPolicy

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user  = user
      @scope = scope
    end

    def resolve
      if scope.present?
          Eoi.by_user_id(user.id)
        # end
      else
        []
      end
    end

  end

  def index?
    user.profile.project.id == Eoi.project_id? or user.id == Eoi.user_id?
  end

  def new?
    true
  end

  def show?
    record.user_id == user.id || user.profile.project_id == record.project_id
    # user.profile.project.id == @eoi.project_id? or user.id == eoi.user_id?
  end

  def edit?
    user.id == eoi.user.id?
  end

  def create?
    true
  end

  def update?
    user.id == eoi.user.id?
  end

  def destroy?
    user.id == eoi.user.id?
  end


end

Routes

resources :eois

resources :projects do
    member do
    resources :eois, controller: 'projects/eois
  end

当我想要显示与项目相关的提交的 EoIs 时,我使用项目 Eoi 策略;当我想要显示用户创建的 Eoi 时,我使用 Eoi 策略——无范围。

我很想弄清楚这一点,这样我就可以按照预期的方式使用这个宝石。非常感谢您的建议。我确信这种尝试不是 Pundit 的目的 - 但我不知道如何使用文档中所示的这个 gem。

我无法使用policy_scope,因为我需要将project_id 参数传递到项目eoi 控制器索引操作的索引操作中。

PaReeOhNos 建议

下面列出了我尝试实施 PareeOhNos 建议的尝试。我不确定我是否正确理解它,因为 eois 总是有一个项目 id 和一个用户 id,但也许我不明白 load_parent 方法正在做什么。

在我的 Eois 控制器中,我有:

class EoisController < ApplicationController
  before_action :load_parent
  before_action :load_eoi, only: [:show, :edit, :update, :destroy]



  def index
    authorize @parent
    @eois = EoiPolicy::Scope.new(current_user, @parent).resolve
  end



  def show

  end

  # GET /eois/new
  def new
    @project = Project.find(params[:project_id])
    @eoi = @project.eois.build
    @contribute = params[:contribute] || false
    @participate = params[:participate] || false
    @partner = params[:partner] || false
    @grant = params[:grant] || false
    @invest = params[:invest] || false
  end

  # GET /eois/1/edit
  def edit
  end

  # POST /eois
  # POST /eois.json
  def create
    @eoi = Project.find(params[:project_id]).eois.build(eoi_params)
    @eoi.user_id = @current_user.id

    respond_to do |format|
      if @eoi.save
        format.html { redirect_to Project.find(params[:project_id]), notice: 'Eoi was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @eoi.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /eois/1
  # DELETE /eois/1.json
  def destroy
    @eoi.destroy
    respond_to do |format|
      format.html { redirect_to @project, notice: 'Eoi was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    def load_parent
      # @parent = (params[:project_id] ? Project.find(params[:project_id] : current_user)
      @parent =  params[:project_id] ? Project.find(params[:project_id]) : current_user
    end

    def load_eoi
      @eoi = Eoi.find(params[:id])
      authorize @eoi
    end

在我的 Eoi 政策中,我有:

class EoiPolicy < ApplicationPolicy
class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user  = user
      @scope = scope
    end

    def resolve
      if scope.is_a?(User)
        Eoi.where(user_id: scope.id)
      elsif scope.is_a?(Project)
        Eoi.where(project_id: scope.id)
      else
        []
      end
    end

  end

  def index?
    record.is_a?(User) || user.profile.project.id == record.project_id
  end

  def new?
    true
  end

  def show?
    record.user_id == user.id || user.profile.project_id == record.project_id
  end

  def edit?
    user.id == eoi.user.id?
  end

  def create?
    true
  end

  def update?
    user.id == eoi.user.id?
  end

  def destroy?
    user.id == eoi.user.id?
  end


end

在我的routes.rb中,我有:

resources :projects do
    member do
  resources :eois, shallow: true

resources :eois, only: [:index]

在我的 eois/index 中,我有:

    <% @eois.sort_by(&:created_at).in_groups_of(2) do |group| %>
        <% group.compact.each do |eoi| %>
            <h4><%= link_to eoi.user.full_name %></h4>
            <%= link_to 'VIEW DETAILS', eoi_path(eoi), :class=>"portfolio-item-view" %>
<% end %>  
<% end %>  

在我的 eois/show 中,我有:

"test"

当我尝试所有这些时,eois/index 页面会加载。当我尝试显示特定的 eoi 页面时,收到一条错误消息:

wrong number of arguments (given 2, expected 0)

错误消息指向授权控制器的@eoi行:

def load_eoi
      @eoi = Eoi.find(params[:id])
      authorize @eoi
    end

如果我将授权@eoi放在显示操作而不是加载eoi方法中,则会出现相同的错误。

申请政策有

class ApplicationPolicy
  attr_reader :user,  :scope

  class Scope
    def initialize(user, scope)
      #byebug        
      @user = user
      # record = record
      @scope = scope
    end

    def resolve
      scope
    end
  end

  def index?
    false
  end

  def show?
    scope.where(:id => record.id).exists?
  end

  def create?
    false
  end

  def new?
    create?
  end

  def update?
    false
  end

  def edit?
    update?
  end

  def destroy?
    false
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

下一次尝试

采纳 PaReeOhNos 的建议(上面复制的),我尝试对其进行一些调整以更好地适合我的用例。

我现在有:

Eoi控制器

class EoisController < ApplicationController
  # before_action :get_project
  # before_action :set_eoi, only: [:show, :edit, :update, :destroy]
  before_action :load_parent
  before_action :load_eoi, only: [:show, :edit, :update, :destroy]


  # GET /eois
  # GET /eois.json
  # def index
  #   @eois = @project.eois
  #   # @eois = Eois.find_by_project_id(params[:project_id])
  # end

  def index
    # authorize @parent
    @eois = policy_scope(Eoi.where(project_id: params[:project_id]))
    # @eois = EoiPolicy::Scope.new(current_user, @parent).resolve
  end


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

  end

  # GET /eois/new
  def new
    @project = Project.find(params[:project_id])
    @eoi = @project.eois.build
    @contribute = params[:contribute] || false
    @participate = params[:participate] || false
    @partner = params[:partner] || false
    @grant = params[:grant] || false
    @invest = params[:invest] || false
  end

  # GET /eois/1/edit
  def edit
  end

  # POST /eois
  # POST /eois.json
  def create
    @eoi = Project.find(params[:project_id]).eois.build(eoi_params)
    @eoi.user_id = @current_user.id

    respond_to do |format|
      if @eoi.save
        format.html { redirect_to Project.find(params[:project_id]), notice: 'Eoi was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @eoi.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /eois/1
  # DELETE /eois/1.json
  def destroy
    @eoi.destroy
    respond_to do |format|
      format.html { redirect_to @project, notice: 'Eoi was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    def load_parent
      # @parent = (params[:project_id] ? Project.find(params[:project_id] : current_user)
      @parent = params[:project_id] ? Project.find(params[:project_id]) : current_user
    end

    def load_eoi
      @eoi = Eoi.find(params[:id])
      # authorize @eoi
    end

EOI政策

class EoiPolicy < ApplicationPolicy

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user  = user
      @scope = scope
    end

    def resolve
      # since we send the scoped eois from controller, we can pick
      # any eoi and get its project id

      # check if the current user is the owner of the project
    #   if (user.profile.projects.map(&:id).include?(project_id))
    #     # user is the owner of the project, get all the eois 
    #     scope.all 
    #   end
    #   #not the owner , then get only the eois created by the user
    #   scope.where(user_id: user.id)
    # end 
      if scope.is_a?(User)
        Eoi.where(user_id: scope.id)
      elsif scope.is_a?(Project) && (user.profile.projects.map(&:id).include?(project_id))
        project_id = scope.first.project_id 
        Eoi.where(project_id: scope.id)
      else
        Eoi.none
      end
    end

  end

  def index?
    record.is_a?(User) || user.profile.project.id == record.project_id
  end

  def new?
    true
  end

  def show?
    record.user_id == user.id || user.profile.project_id == record.project_id
  end

  def edit?
    user.id == eoi.user.id?
  end

  def create?
    true
  end

  def update?
    user.id == eoi.user.id?
  end

  def destroy?
    user.id == eoi.user.id?
  end


end

Routes

resources :eois#, only: [:index]
  concern :eoiable do
    resources :eois
  end

resources :projects do
    concerns :eoiable
  end

Index

   <% @eois.sort_by(&:created_at).in_groups_of(2) do |group| %>
     <% group.compact.each do |eoi| %>
     <h4><%= link_to eoi.user.full_name %></h4>
     <%= link_to 'VIEW DETAILS', project_eoi_path(eoi.project, eoi), :class=>"portfolio-item-view" %>
                            <% end %>  
                        <% end %>   

View

'test'

这是行不通的,因为当我导航到一个项目,然后尝试渲染具有匹配项目 id 的 eois 索引时,当我的数据库中有 4 条应该渲染的记录时,我会得到一个空的索引页。

莱托的建议

采纳 Leito 的建议,我也尝试过:

Eoi控制器

class EoisController < ApplicationController
  before_action :get_project
  before_action :set_eoi, only: [:show, :edit, :update, :destroy]
  # before_action :load_parent
  # before_action :load_eoi, only: [:show, :edit, :update, :destroy]


  # GET /eois
  # GET /eois.json
  # def index
  #   @eois = @project.eois
  #   # @eois = Eois.find_by_project_id(params[:project_id])
  # end

  def index
    # authorize @eois
    # authorize @parent
    # policy_scope(@project.eois)
    @eois = policy_scope(Eoi.where(project_id: params[:project_id]))
    # @eois = EoiPolicy::Scope.new(current_user, @parent).resolve
  end


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

  end

  # GET /eois/new
  def new
    @project = Project.find(params[:project_id])
    @eoi = @project.eois.build
    @contribute = params[:contribute] || false
    @participate = params[:participate] || false
    @partner = params[:partner] || false
    @grant = params[:grant] || false
    @invest = params[:invest] || false
  end

  # GET /eois/1/edit
  def edit
  end

  # POST /eois
  # POST /eois.json
  def create
    @eoi = Project.find(params[:project_id]).eois.build(eoi_params)
    @eoi.user_id = @current_user.id

    respond_to do |format|
      if @eoi.save
        format.html { redirect_to Project.find(params[:project_id]), notice: 'Eoi was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @eoi.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /eois/1
  # DELETE /eois/1.json
  def destroy
    @eoi.destroy
    respond_to do |format|
      format.html { redirect_to @project, notice: 'Eoi was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    # def load_parent
    #   # @parent = (params[:project_id] ? Project.find(params[:project_id] : current_user)
    #   @parent = params[:project_id] ? Project.find(params[:project_id]) : current_user
    # end

    # def load_eoi
    #   @eoi = Eoi.find(params[:id])
    #   # authorize @eoi
    # end
    # # Use callbacks to share common setup or constraints between actions.
    def set_eoi
      @eoi = Eoi.find(params[:id])
    end

    def get_project
      @project = Project.find(params[:project_id])
    end

意欧伊政策

def initialize(user, scope)
      @user  = user
      @scope = scope
    end

    def resolve

      if scope.joins(project: :profile).where profiles: { user_id: user }
        Eoi.where(project_id: scope.ids)
      elsif scope.joins(eoi: :user).where eois: { user_id: user }  
        Eoi.where(user_id: scope.ids)
      else
        Eoi.none
      end  
      # since we send the scoped eois from controller, we can pick
      # any eoi and get its project id

      # check if the current user is the owner of the project
    #   if (user.profile.projects.map(&:id).include?(project_id))
    #     # user is the owner of the project, get all the eois 
    #     scope.all 
    #   end
    #   #not the owner , then get only the eois created by the user
    #   scope.where(user_id: user.id)
    # end 
      # if scope.is_a?(User)
      #   Eoi.where(user_id: scope.id)
      # elsif scope.is_a?(Project) && (user.profile.projects.map(&:id).include?(project_id))
      #   project_id = scope.first.project_id 

      #   Eoi.where(project_id: scope.id)
      # else
      #   Eoi.none
      # end
    end

  end

  def index?
    true
    # record.is_a?(User) || user.profile.project.id == record.project_id
  end

  def new?
    true
  end

  def show?
    true
    # record.user_id == user.id || user.profile.project_id == record.project_id
  end

  def edit?
    user.id == eoi.user.id?
  end

  def create?
    true
  end

  def update?
    user.id == eoi.user.id?
  end

  def destroy?
    user.id == eoi.user.id?
  end


end

路线和视图与上面的尝试相同

这里的问题是我的控制器中的 get project 方法。我需要这个场景来显示特定项目的所有 eois。当我试图显示所有用户的 eois 时,我不需要它。

当我保存所有这些并尝试时,项目上的 eois 显示正确。然而,应该向我显示所有(作为用户)eois 的 eois(未嵌套在项目内)显示了一个错误:

Couldn't find Project with 'id'=

错误消息突出显示“get_project 方法”。

LEITO 的最新建议

采纳Leito更新的建议,我列出了当前的尝试。

在此之前,我想澄清一下,所有 Eois 都将具有用户 ID 和项目 ID。我使用这张表供用户表达对项目的兴趣。我的目标是让其个人资料拥有该项目的用户查看该项目上提交的所有 eois。然后,我还希望用户看到他们自己提交的所有 eois(跨所有项目)。

意欧伊政策

def resolve
  if scope.joins(project: :profile).where 'profiles.user_id = ? OR eois.user_id = ?', user.id, user.id
   Eoi.all
  else
    Eoi.none
  end  

Eoi控制器

def index
    @eois = policy_scope(Eoi)
    @eois = @eois.where(project_id: params[:project_id]) if params[:project_id]
  end

目前,这可以很好地查找嵌套在项目 (project/26/eois) 下的 eois。但是,当我尝试执行 eois/index (未嵌套在项目下)时,我想返回所有用户的 eois,我收到一条错误消息:

Couldn't find Project with 'id'=

它突出显示了 eoi 控制器的这一行:

def get_project
  @project = Project.find(params[:project_id])
end

我不确定我现在是否理解解析方法或控制器剔除的想法。我看不出范围线有什么问题,无法尝试更改哪些内容。


我是该问题的前评论者。

对于您的 EoiScope,您只需要用户可以访问哪些 Eois(因为它们属于此配置文件下的项目),独立于项目(此要求仅针对控制器,因为是嵌套的),因此您的控制器应该类似于这:

Edit:根据您最近的尝试,我已经更新了范围以考虑直接属于用户(而不是通过项目)的 Eois,您应该简单地将其范围限制到项目或不基于 params[:project_id] 的存在,查看更新的答案。

@eois = policy_scope(Eoi)
@eois = @eios.where(project_id: params[:project_id]) if params[:project_id]

并且您的范围应该进行连接,直到到达用户为止,或者只是在 Eoi 上查找 user_id 属性。

  class EoiPolicy < ApplicationPolicy
    class Scope < Scope
      def resolve
        scope.joins(project: : profile).where 'profiles.user_id = ? OR eois.user_id = ?', user.id, user.id
      end
    end

    # Other methods that differ from ApplicationPolicy's methods
  end

请注意,Scope 没有调用eoi,但默认*范围只知道scope and user。 * 默认情况下,我的意思是当它继承自ApplicationPolicy::Scope

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails 4 - Pundit - 索引范围策略 的相关文章

  • “没有可用的二元红宝石”是什么意思?

    每当我使用rvm install x x x 即使安装成功 我也会收到此警告 No binary rubies available for osx 10 12 x86 64 ruby 2 4 0 Continuing with compil
  • Rails:format.js 或 format.json,或两者?

    可能很明显 但我仍然缺乏基本知识 那么在控制器内部 两者都可以使用 还是总是 Javascript 所以两者是相同的 json and js是两种不同类型的响应 它们在 Rails 中被定义为不同的 MIME 类型 Mime Type re
  • 可以覆盖/实现的 ruby​​ 运算符列表

    是否有可以覆盖的所有 ruby 运算符的列表 不是那些不能的 Here s Ruby 运算符表 http phrogz net programmingruby language html table 18 4 方法和可重载的有 Elemen
  • 预期的 ProductField,出现数组问题

    我有一个 Rails 4 应用程序 它有一个如下所示的 params 块 def store params params require store permit name description user id products attr
  • 如何向 Time.now 添加两周?

    如何在 Ruby 中向当前 Time now 添加两周 我有一个使用 DataMapper 的小型 Sinatra 项目 在保存之前 我有一个字段填充了当前时间加上两周 但未按需要工作 任何帮助是极大的赞赏 我收到以下错误 NoMethod
  • Watir 更改 Mozilla Firefox 首选项

    我正在使用 Watir 运行 Ruby 脚本来自动执行一些操作 我正在尝试自动将一些文件保存到某个目录 因此 在我的 Mozilla 设置中 我将默认下载目录设置为桌面并选择自动保存文件 然而 当我开始运行脚本时 这些更改并未反映出来 似乎
  • Capistrano 和 XSendFile 配置

    我正在尝试使用 Apache 2 2 Passenger 4 0 59 和 XSendFile 0 12 配置 Rails 生产服务器 应用程序通过 Capistrano 部署 部署的应用程序生成 可能很大 PDF Rails root t
  • Ruby on Rails REST 设计问题 - 在账户之间转账

    我有一个 Account 类 想要实现转账屏幕以允许用户在 2 个账户之间转账 我将如何实现这种 RESTful 方式 我有标准帐户和休息操作 那很好 但我该如何实现转移呢 通常我只会向帐户控制器和相应的视图添加一个名为 transfer
  • Rails 3 + angularjs + 缩小在生产中不起作用:未知提供者:eProvider

    我已遵循我能找到的所有修复缩小的说明 例如 var MyController function renamed scope renamedGreeter MyController inject scope greeter and someM
  • 遵循 http://ruby.railstutorial.org/ 教程时出现 RSpec 错误

    我到处搜索但似乎找不到解决方案 我一直在关注有关的教程http ruby railstutorial org http ruby railstutorial org 我在第 3 章中尝试使用 rspec spec 执行测试 但是我不断收到以
  • 全局变量声明

    我是 Python 的初学者 并且已经处理过全局变量的概念 当我以为我理解了这个概念时 我看到了一段简短的代码 证明我错了 message global def enclosure message enclosure def local g
  • 如何从 Ruby 程序发送邮件?

    我想从 Ruby 应用程序发送电子邮件 核心语言中是否有调用来执行此操作 或者是否有我应该使用的库 最好的方法是什么 如果你不想使用行动邮递员 http wiki rubyonrails org rails pages ActionMail
  • 如何从引擎覆盖 Rails 应用程序路由?

    我有一个 Rails 应用程序 我正在尝试将 Rails 引擎集成到其中 主机应用程序有一些捕获所有路由 magic urls match gt admin rendering show match path edit gt admin r
  • 测量两个字符串之间相似性的有效方法是什么? (编辑距离使堆栈太深)

    所以 我从这个开始 http en wikibooks org wiki Algorithm Implementation Strings Levenshtein distance Ruby http en wikibooks org wi
  • ruby 中的 #encode 和 #force_encoding 有什么区别?

    我真的不明白之间的区别 encode and force encoding在 Ruby 中String班级 我明白那个 kam force encoding UTF 8 将迫使 kam 是UTF 8编码 但是怎么样 encode encod
  • 如何使用 Rspec 测试具有嵌套路由的控制器?

    我有 2 个使用轨道脚手架生成器创建的控制器 我希望它们嵌套在一个名为 demo 的文件夹中 所以运行 rails g scaffold demo flows rails g scaffold demo nodes 然后我决定将节点嵌套在流
  • “rmagick”gem 安装问题

    我在尝试在 centos 上安装 rmagick gem 时遇到问题 以下是我得到的输出 谁能帮我识别一下我缺少什么包裹 我已经安装了所有提到的另一个堆栈溢出线程 RMagick安装错误 https stackoverflow com qu
  • 我在 Rails 中使用了保留字吗?

    这是我的模型 class Record lt ActiveRecord Base belongs to user belongs to directory end class Directory lt ActiveRecord Base h
  • REXML - 如何提取单个元素

    我正在用 ruby 编写一些验收测试 其中涉及断言响应 XML 中值的存在 我的 XML 是这样的
  • 如何为 jQuery 插件设置私有变量?

    我想创建一个简单的插件 它使用元素的文本作为默认值 或者您可以在调用插件时设置此值 但是 如果我不设置该值 并为多个元素调用插件 则默认值会成倍增加 function fn reText function options var setti

随机推荐

  • 接近大小限制 - 手表应用程序的大小(50MB 限制令人头疼。)

    我们的应用程序达到了大约 49MB 而且我们还没有完成一半 所以肯定会超过50MB的限制 我有以下几个问题 1 watchOS 中可以使用按需资源吗 2 我的资源 图像 自定义字体 制作2份副本 每一份是手表应用程序 另一份是手表扩展 怎么
  • 如何获取 Scala 中当前的脚本或类名?

    我希望我的 Scala 程序能够以编程方式确定其脚本文件名或其类名 将字符串存储在变量中program并打印出名字 Java有several为此的方法 我认为这是最简单的 val program new Exception getStack
  • 为什么模板函数调用不明确?

    include
  • 为什么 strchr 比我的 simd 代码快两倍

    我正在学习 SIMD 并且很好奇是否有可能在查找字符方面击败 strchr 看起来 strchr 使用相同的内在函数 但我假设它检查空值 而我知道该字符在数组中并计划避免空值检查 我的代码是 size t N 1e9 bool found
  • Android 中的垂直画廊

    我正在开发一个应用程序 为此我需要将图库设置为垂直模式而不是水平模式 我的问题是 我是否有可能展示垂直画廊类型的小部件 任何帮助表示赞赏 你为什么不创建一个列表视图的自定义适配器 如果你定义一个具有一个图像视图的自定义适配器那么它将与垂直画
  • 按内容查找 html 元素并使用 jQuery 隐藏

    我正在尝试使用 jquery 查找某些 HTML 中的某些元素 我想查找并隐藏包含标签文本 这是我的测试标签 和 Yest another test label 的列表 id ul class top level list li class
  • 如何在Excel VBA中给出小于一秒的时间延迟?

    我想在小于 1 秒的特定持续时间后重复一个事件 我尝试使用以下代码 Application wait Now TimeValue 00 00 01 但这里的最小延迟时间是一秒 如何延迟半秒 您可以使用 API 调用和睡眠 将其放在模块的顶部
  • 使用displaytag+tiles导出excel的问题

    显示标签 1 2 弹簧 MVC 2 5 6 瓷砖2 0 7 我已经查看了该网站上围绕同一主题区域的其他一些项目 但似乎没有回答如何将显示标记生成的表格导出到 Excel 的问题 我可以导出 保存 xml 和 csv 但不能导出 保存 exc
  • Xcode iOS 按下按钮,然后向上拖动第二个按钮

    假设我想给一个整数加 1 这只有当我按下一个按钮时才会完成UIButton然后松开我的手指到另一个UIButton 拖拽组合 我可以做的最简单的方法是什么IBAction出现在组合中 这可以通过触摸坐标来完成 或者只是UIButtons a
  • 如何在没有时间的情况下获取当前日期?

    我可以使用以下方法获取日期和时间 DateTime now DateTime Now 如何以 DateTime 格式单独获取当前日期和时间 我没有使用 ASP NET C 中的日期时间选择器对话框 好吧 您可以将今天的日期作为DateTim
  • PHPmail 函数中的“无法访问文件:”

    我正在尝试使用 PHPMailer 通过电子邮件发送服务器上存在的文件 当我运行此代码时 我收到 无法访问文件 并且发送的电子邮件没有附件 任何人都可以指导我如何解决此问题 checkyes POST check date date Y m
  • Yahoo! 上的正则表达式管道

    我在胡闹雅虎 管道我在一些正则表达式方面碰壁了 现在我熟悉了 Perl 中的正则表达式 但 Yahoo 中的规则似乎有所不同 管道 我正在做的是获取一个页面并尝试将其转换为提要 我的正则表达式用于从 HTML 中删除链接 效果很好 但我想要
  • Inno Setup - 从外部源(文件或文件夹内容)创建组件/类型的动态列表

    我有一个批处理文件 设置更改器 它使用 xcopy 列出特定文件夹中的特定文件格式 然后允许我输入其中一个名称 脚本使用该名称将该文件复制到另一个位置 首先 xcopy 创建原始副本作为备份 滚动备份仅 1 份 然后进行文件复制 扩展名在批
  • 错误 A2070:在汇编语言中使用嵌套 while 循环时指令操作数无效

    我正在尝试使用 Masm 在汇编中嵌套 while 循环 我在第 15 行 即运行以下代码时内部 while 循环的 endw 指令 收到 错误 A2070 无效指令操作数 INCLUDE Irvine32 inc data i byte
  • apache 无法正确提供静态内容

    我一直在开发自己的 mvc 框架来进一步学习 Web 应用程序 但在提供静态资源时遇到问题 我试图在应用程序中有一个入口点 也称为前端控制器 所以在我的项目中 我有一个 htaccess 文件 它将所有请求重定向到 app 文件夹 其中另一
  • 将两个不同的结构传递给同一个函数

    我有 2 个不同大小的结构 我希望有一个可以将它们传递到其中的函数 但是 我不知道如何定义函数的参数来接受 2 个不同的结构 我的结构如下 struct int a 2 byte int b 2 byte int c 2 byte int
  • 已部署动态 Web 项目的 WebSphere ClassNotFoundException

    Problem 我目前在 WebSphere 7 0 应用程序服务器上部署的 EAR 带有 OpenFaces Web 项目 遇到 ClassNotFoundException EAR 部署没有问题 但是当我从 Web 项目 包含在 EAR
  • 警告:函数“strcmp”的隐式声明[重复]

    这个问题在这里已经有答案了 创建一个简单的代码来扫描两个数字 询问用户是否想要将它们相加或相乘 然后执行运算并打印输出 include
  • BorderLayout 无法正确显示

    我想要一个 JFrame 其中左侧和右侧有一个边框 颜色为黑色 宽度为 withfOfJFrame 10 现在 我的尝试如下所示 JFrame f new JFrame f setSize 800 600 f setLayout new B
  • Rails 4 - Pundit - 索引范围策略

    我正在尝试学习如何将 Pundit 与我的 Rails 4 应用程序一起使用 我有以下型号 class User lt ActiveRecord Base has one profile has many eois end class Pr