Rails 3 - 找不到没有ID的<controller> </controller>

时间:2011-11-26 12:29:57

标签: ruby ruby-on-rails-3 routes

我创建了一个新的控制器并添加了一个新动作( my_new_action )。对于routes.rb,我添加了此操作的路由,但是当我在URL浏览器中设置地址 localhost:3000 / users / my_new_action 时,我将收到主题中的错误。< / p>

class UsersController < ApplicationController
  # ....

  def my_new_action  
  end
end

有人可以帮助我,如何解决?

由于

修改

  resources :users do
    collection do
      get 'my_new_action'
    end
  end

EDIT2 这是终端的确切输出:

    Started GET "/users/my_new_action" for 127.0.0.1 at Sat Nov 26 13:55:50 +0100 2011
  Processing by UsersController#my_new_action as HTML
filter_access_to tried to find User from params[:id] (nil), because attribute_check is enabled and @user isn't set, but failed: ActiveRecord::RecordNotFound: Couldn't find User without an ID
Completed 404 Not Found in 1ms

ActiveRecord::RecordNotFound (Couldn't find User without an ID):


Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.8ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (6.0ms)

2 个答案:

答案 0 :(得分:2)

标题具有误导性,您放置<controller>因为您可能看到User,但它很可能是指您正在尝试构建或使用的模型。因为你没有粘贴更多的代码,所以我不能确切地说出问题的位置,而是寻找对你在代码中使用的id的引用 - 这很可能是null。

答案 1 :(得分:0)

您收到错误是因为路由无效,代码显示的是url / users / my_new_action正在尝试为默认的“show”找到'/ users / my_new_action'的用户ID动作。

我会将您提供的路线代码替换为类似的东西(请注意,这不是经过测试的代码)......

resources :users do
    match '/my_new_action', :controller => 'users', :action => 'my_new_action', :as => :mynewaction
end