加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 02:15:01 所属栏目:百科 来源:网络整理
导读:首先,我在 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' % 它返回此错误.为什么? 记
首先,我在 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,Order,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读