简体   繁体   English

Rails 3 NameError(未初始化的常量 <controllerName> )

[英]Rails 3 NameError (uninitialized constant <controllerName>)

I get a NameError uninitialized constant WorkoutLog when attempting to access a non-resourceful action on a controller. 尝试访问控制器上的非资源操作时,我得到了NameError uninitialized constant WorkoutLog

My controller looks as follows: 我的控制器如下所示:

class WorkoutLogController < ApplicationController
   def index
     # random code
   end

   def on_date
     # random code
   end
end

My Routes File: 我的路线文件:

 match "workout_log/:date", :controller => "workout_log", :action => "on_date", :as => "log_on_date"
 match "workout_log", :controller => 'workout_log', :action => 'index'

And then I have a my link_to as such: 然后我有一个我的link_to:

<%= link_to "View Log", log_on_date_path(:date => Date.today.strftime('%d-%B-%Y')), :remote => true, "data-type" => "html" %>

The WorkoutLogController has no model behind it - it's just a controller. WorkoutLogController背后没有模型-它只是一个控制器。 However, I can't perform a request on the on_date because it throws the following error: 但是,我无法在on_date上执行请求,因为它引发以下错误:

NameError (uninitialized constant WorkoutLog):

When I run rake routes it seems to be fine as it generates the following: 当我运行rake routes时,它会生成以下内容,这似乎很好:

log_on_date        /workout_log/:date(.:format)                  {:controller=>"workout_log", :action=>"on_date"}
workout_log        /workout_log(.:format)                        {:controller=>"workout_log", :action=>"index"}

I can't wrap my head around what the issue is (especially after spending the past night trying to figure it out). 我无法全神贯注于问题所在(尤其是在过去一夜试图解决问题之后)。 Is rails looking for a model to associate with it and failing to do so? Rails是否正在寻找与之关联的模型而没有这样做?

I figured out the issue. 我发现了问题。 It had to do with declarative_authorization. 它与declarative_authorization有关。
Since I had not copied the code 100%, I left out the following from my controller code pasted above: 由于我没有100%复制代码,因此我从上面粘贴的控制器代码中省略了以下内容:

filter_resource_access

declarative_authorization uses this for resourceful routes - which was not my case. declarative_authorization将此用于资源丰富的路由-这不是我的情况。 I've since switched it to filter_access_to :all and it works fine. 从那以后,我将其切换为filter_access_to :all ,并且工作正常。 I got to the root of the problem by creating the file workout_log.rb in my models: 通过在模型中创建文件workout_log.rb ,我找到了问题的根源:

class WorkoutLog
end

Then when I issued a request to the index and on_date actions, it gave me the error that it could not find ActiveModel methods on WorkoutLog, which in turn pointed me to the filters on the WorkoutLogController. 然后,当我向index和on_date操作发出请求时,它给了我一个错误,即它在WorkoutLog上找不到ActiveModel方法,这又使我指向了WorkoutLogController上的筛选器。

If you look under Examples > Controller at the declarative_authorization page on GitHub , it'll provide the necessary info. 如果您查看GitHub上declarative_authorization页上的 Examples> Controller下的内容,它将提供必要的信息。 Hope this helps anyone else who has this issue! 希望这对其他遇到此问题的人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM