Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 don';t的工作能力属于模型_Ruby On Rails 3_Cancan - Fatal编程技术网

Ruby on rails 3 don';t的工作能力属于模型

Ruby on rails 3 don';t的工作能力属于模型,ruby-on-rails-3,cancan,Ruby On Rails 3,Cancan,我在使用CanCan gem时遇到一些问题 我有能力.rb文件: if user.nil? can :read, :all elsif user.admin? can :manage, Publication else can [:read, :create], Publication can [:update, :destroy], Publication, :user_id => user.id end 它是出版物。rb: attr_accessible :con

我在使用CanCan gem时遇到一些问题

我有能力.rb文件:

if user.nil?
  can :read, :all
elsif user.admin?
  can :manage, Publication
else
  can [:read, :create], Publication
  can [:update, :destroy], Publication, :user_id => user.id
end
它是出版物。rb

  attr_accessible :content,:title

   belongs_to :user

   validates :user_id, presence: true

   validates :title, presence: true, length: { maximum: 140 }

   validates :content, presence: true, length: { minimum: 240 }

   default_scope order: 'publications.created_at DESC'
它是index.html.erb,用于出版物:

    <% @publications.each do |publicate| %> 

    <h3><%= publicate.title %></h3>

        <% if can? :update, :destroy, Publication %>
          <%= link_to "Update", edit_publication_path(publicate) %>
          |<%= link_to " delete", publicate, method: :delete,
                              data: { confirm: "Are you sure?" } %>
        <% end %>
    <% end %>
出版物
用户
,它的作品,我在用户视图中看到删除的链接:

  <% @users.each do |user| %>
  <li>
    <%= link_to user.username, user %>
    <% if can? :destroy, user %>
      | <%= link_to "delete", user, method: :delete,
                                  data: { confirm: "Are you sure?" } %>
    <% end %>
  </li>
<% end %>

  • |
  • 而且
    user.admin可以:管理,:all
    ,它也可以与用户和出版物一起使用。为什么可以忽略出版能力?

    请阅读:

    你应该明白为什么你的条件反射

    can? :update, :destroy, Publication
    
    这是不正确的。您的操作是
    :update
    ,主题是
    :destroy
    ,您的第三个参数
    Publication
    基本上被忽略

    can? :update, :destroy, Publication