即使指定了路由,Rails 3.1 自定义控制器操作也会不断询问 ID

发布于 2024-11-07 09:33:02 字数 1720 浏览 2 评论 0原文

我正在尝试向控制器添加自定义操作(“last_ Five”)。

我的路线指定为:(

people_last_five GET  /people/last_five(.:format)  {:action=>"last_five", :controller=>"people"}

即,这是 rake_routes 的输出)。

但是当我浏览到 /people/last_ Five 时,出现以下错误。

于 2011 年 5 月 15 日星期日 22:03:18 +0000 开始为 XXX.XX.XXX.XXX 获取“/people/last_ Five” 由 PeopleController#last_ Five 作为 HTML 进行处理 用户负载(1.4ms)^[[0m SELECT users.* FROM users WHERE users.id = 3限制1 86 毫秒内完成 ActiveRecord::RecordNotFound(无法找到没有 ID 的人员):

我认为这是我的routes.rb中的问题

在我的routes.rb中我目前有:

get 'people/last_five'
resources :people

我也尝试过

resources :people do
  get 'last_five', :on => collection
end

,但给出了相同的结果。

当路线中没有“/:id/”时,为什么 Rails 会尝试获取 ID?

当我将路线指定为“/people/:id/last_ Five”并向其传递虚拟 ID 时,甚至会发生这种情况。在这种情况下,它仍然告诉我 ActiveRecord::RecordNotFound (无法找到没有 ID 的人)。

即使我将操作本身减少为调试存根,我也遇到这个问题,所以我认为这不是问题。在我的控制器中:

  # GET /people/last_five                                                                                                                                                 
def last_five
  logger.info "LAST FIVE IS BEING CALLED"
  #@people = Person.last_five
  #respond_with @people do |format|
  #   format.json { render :json => @people }
  #end
end

知道这里发生了什么吗?看起来rails 被告知要通过routes.rb 之外的东西获取ID。我已经找遍了所有我能想到的地方。

任何线索都将受到高度赞赏。

谢谢。

编辑: 我的 PeopleController 开始如下:

before_filter :authenticate_user!, :except => []
filter_resource_access
respond_to :html, :js, :json

I'm trying to add a custom action ('last_five') to a controller.

My routes are specified as:

people_last_five GET  /people/last_five(.:format)  {:action=>"last_five", :controller=>"people"}

(i.e. that's the output of rake_routes).

But when I browse to /people/last_five I get the following error.

Started GET "/people/last_five" for XXX.XX.XXX.XXX at Sun May 15 22:03:18 +0000 2011
Processing by PeopleController#last_five as HTML
User Load (1.4ms)^[[0m SELECT users.* FROM users WHERE users.id = 3 LIMIT 1
Completed in 86ms
ActiveRecord::RecordNotFound (Couldn't find Person without an ID):

I thought this was a problem in my routes.rb

In my routes.rb I currently have:

get 'people/last_five'
resources :people

I've also tried

resources :people do
  get 'last_five', :on => collection
end

but that gives the same results.

Why is rails trying to get an ID when there is no "/:id/" in the route?

This even happens when I specify the route as '/people/:id/last_five' and pass it a dummy id. In that case it still tells me ActiveRecord::RecordNotFound (Couldn't find Person without an ID).

I have this problem even when I reduce the action itself to a stub for debugging, so I don't think that's the problem. In my controller:

  # GET /people/last_five                                                                                                                                                 
def last_five
  logger.info "LAST FIVE IS BEING CALLED"
  #@people = Person.last_five
  #respond_with @people do |format|
  #   format.json { render :json => @people }
  #end
end

Any idea what's going on here? It seems like rails is being told to get an ID by something outside of routes.rb. I've looked everywhere I can think.

Any leads are HIGHLY appreciated.

Thanks.

EDIT:
My PeopleController begins like so:

before_filter :authenticate_user!, :except => []
filter_resource_access
respond_to :html, :js, :json

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

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

发布评论

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

评论(2

女皇必胜 2024-11-14 09:33:02

根据对您问题的讨论,原因是之前/周围过滤器干扰,而不是您的特定操作问题。您的应用程序正在搜索用户,因此它可能与身份验证相关。

Per the discussion on your questions, the cause is a before/around filter interfering rather than an issue with your specific action. Your application is searching for a User, so it may be authentication-related.

莳間冲淡了誓言ζ 2024-11-14 09:33:02

您确定这是在控制中,而不是在模型中吗? Rails 不希望 Model 的东西在 Control 中。

Are you sure this goes in Control, and not in Model? Rails doesn't want Model stuff in Control.

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