ruby-on-rails – 当我使用CanCan时,为什么会出现Name Error

首先,我在
http://example–site.com/shop/walmart/order/1登录

如果我按下这样的代码所做的编辑按钮.

  <%= link_to t('.edit', :default => t("helpers.links.edit")),
              edit_community_order_path(@community, @order), :class => 'btn' %>

它返回此错误.为什么?

记录错误(名称错误!)

Started GET "/shop/walmart/order/1/edit"Processing by OrdersController#edit as HTML
  Parameters: {"community_id"=>"walmart", "id"=>"1"}
  [1m[36mCommunity Load (0.5ms)[0m  [1mSELECT `communities`.* FROM `communities` WHERE `communities`.`community_name` = 'walmart' AND (`communities`.`deleted_at` IS NULL) LIMIT 1[0m
  [1m[35mUser Load (0.4ms)[0m  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 AND (`users`.`deleted_at` IS NULL) LIMIT 1
Completed 500 Internal Server Error in 74ms

NameError (uninitialized constant Ability::Order):
  app/models/ability.rb:26:in `initialize'
  app/controllers/application_controller.rb:21:in `new'
  app/controllers/application_controller.rb:21:in `current_ability'

这是我的代码.

app / models / ability.rb:26:在`initialize’中

    if user
        can :read, :all 
        can [:create, :destroy], Comment, {:user_id => user.id}
        can [:create, :update], Community, {:user_id => user.id}
26>     can [:create, :update], Order, {:user_id => user.id}
        can [:create, :update], CommunityTopic, {:user_id => user.id}
    else
        can :read, :all

    end

app / controllers / application_controller.rb:21:in new’
应用程序/控制器/ application_controller.rb:21:incurrent_ability”

    #CanCan
    private 
    def current_ability 
21>     @current_ability ||= Ability.new(current_user) 
    end

的routes.rb

resources :communities, :path => "shop", do
    resources :orders, :path => "order"
end 

orders_controller.rb

load_and_authorize_resource :community, :find_by => :community_name
load_and_authorize_resource :through => :community, :shallow => true
…..
  # GET /orders/1/edit
  def edit
  end

更新:

车型/ order.rb

class Order < ActiveRecord::Base

  attr_accessible :deleted_at, :code, :community_id, :greeting, :user_id, :visible
  belongs_to :user
  belongs_to :community

  acts_as_paranoid

end

最佳答案

# is there a models/order.rb?
class Order
end
点赞