Rails 3 NameError(未初始化常量

发布于 2025-01-04 16:28:39 字数 1214 浏览 2 评论 0原文

尝试访问控制器上的非资源操作时,我收到 NameError uninitializedconstant WorkoutLog

我的控制器如下所示:

class WorkoutLogController < ApplicationController
   def index
     # random code
   end

   def on_date
     # random code
   end
end

我的路线文件:

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

然后我有一个这样的链接:

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

WorkoutLogController 背后没有模型 - 它只是一个控制器。但是,我无法在 on_date 上执行请求,因为它会引发以下错误:

NameError (uninitialized constant WorkoutLog):

当我运行 rake paths 时,它似乎没问题,因为它会生成以下内容:

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

我无法集中精力问题是什么(特别是在花了昨晚试图弄清楚之后)。 Rails 是否正在寻找与其关联的模型但未能成功?

I get a NameError uninitialized constant WorkoutLog when attempting to access a non-resourceful action on a controller.

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 "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. However, I can't perform a request on the on_date because it throws the following error:

NameError (uninitialized constant WorkoutLog):

When I run rake routes it seems to be fine as it generates the following:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2025-01-11 16:28:39

我想通了这个问题。它与 declarative_authorization 有关。
由于我没有 100% 复制代码,因此我从上面粘贴的控制器代码中遗漏了以下内容:

filter_resource_access

declarative_authorization 将其用于资源丰富的路由 - 这不是我的情况。我已将其切换为 filter_access_to :all 并且工作正常。我通过在模型中创建文件 workout_log.rb 找到了问题的根源:

class WorkoutLog
end

然后,当我向索引和 on_date 操作发出请求时,它给出了找不到 ActiveModel 的错误WorkoutLog 上的方法,这又将我指向 WorkoutLogController 上的过滤器。

如果您查看“示例”>“ GitHub 上的 declarative_authorization 页面 上的控制器,它将提供必要的信息。
希望这对遇到此问题的其他人有所帮助!

I figured out the issue. It had to do with declarative_authorization.
Since I had not copied the code 100%, I left out the following from my controller code pasted above:

filter_resource_access

declarative_authorization uses this for resourceful routes - which was not my case. I've since switched it to filter_access_to :all and it works fine. I got to the root of the problem by creating the file workout_log.rb in my models:

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.

If you look under Examples > Controller at the declarative_authorization page on GitHub, it'll provide the necessary info.
Hope this helps anyone else who has this issue!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文