首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在rails中实现pundit gem的问题

在rails中实现pundit gem的问题
EN

Stack Overflow用户
提问于 2013-10-20 03:35:14
回答 2查看 538关注 0票数 0

我一直试图在rails中恳求权威人士的gem,它没有出现在我的索引页面中,但不确定为什么,我的错误消息说

代码语言:javascript
复制
1) Error:
DeletingAPost Feature Test#test_0001_delete the post:
Capybara::ElementNotFound: Unable to find link or button "Destroy"
    /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/node/finders.rb:41:in `block in find'
    /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/node/base.rb:81:in `synchronize'
    /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/node/finders.rb:30:in `find'
    /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/node/actions.rb:13:in `click_link_or_button'
    /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/session.rb:354:in `block (2 levels) in <class:Session>'
    /Users/cheatermoves/.rvm/gems/ruby-2.0.0-p247/gems/capybara-2.1.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
    /Users/cheatermoves/newHW/Portfolio/test/features/post_delete_post_test.rb:7:in `block (2 levels) in <top (required)>'

这是我的索引html帖子页面的副本。

代码语言:javascript
复制
<h1>Listing posts</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Content</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @posts.each do |post| %>
  <tr id="<%= dom_id(post) %>">
    <td><%= post.title %></td>
    <td><%= post.content %></td>
    <td><%= link_to 'Show', post %></td>
    <% if policy(post).update? %>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <% end %>
    <% if policy(post).destroy? %>
       <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      <% end %>
    </tr>
  <% end %>
</table>

<%= link_to 'New Post', new_post_path %>

这是我的保单页面的副本

代码语言:javascript
复制
class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    raise Pundit::NotAuthorizedError, "must be logged in" unless user
    @user = user
    @record = record
  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
end

class PostPolicy < ApplicationPolicy
  class Scope < Struct.new(:user, :scope)
    def resolve
      scope
    end
  end
end

我用devise安装的用户模型和我的post模型

代码语言:javascript
复制
class User < ActiveRecord::Base
  attr_accessible :role, :published

  def author?
    role == 'author'
  end

  def editor?
    role == 'editor'
  end
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  # attr_accessible :title, :body
  has_many :posts, foreign_key: "author_id"
end


class Post < ActiveRecord::Base
  attr_accessible :content, :title
  belongs_to :author, class_name: "User"

  def publish!
    published = true
    save!
  end
end

还有我的posts控制器

对不起,弄清楚这件事对我来说就像在干草堆里找根针,谁能给我指出正确的方向?

EN

回答 2

Stack Overflow用户

发布于 2014-10-25 02:36:32

很抱歉在老帖子上回答,但是为了这个,你的毁灭?策略自动为false (您没有销毁?在post策略中,您将回退到ApplicationPolicy),因此无论用户是谁,都不会创建delete按钮。

票数 0
EN

Stack Overflow用户

发布于 2013-10-21 12:28:26

您注销了LimeChat,所以我无法回答您的问题,添加到视图的逻辑表明,它们应该只为应该有权访问这些操作的用户填充。因此,您可以尝试将您的登录用户设置为测试的编辑器装置,因为现在理论上,他们是唯一应该看到这些按钮填充的用户。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19470225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档