user984621
user984621

Reputation: 48443

Rails 3 - Couldn't find <controller> without an ID

I have created a new controller and I added the a new action (my_new_action). To the routes.rb I added the route for this action, but when I set to the URL browser the address localhost:3000/users/my_new_action, I will get the error that is in the subject.

class UsersController < ApplicationController
  # ....

  def my_new_action  
  end
end

Could anyone help me please, how to fix it?

Thanks

EDIT

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

EDIT2 This is the exact output from terminal:

    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)

Upvotes: 0

Views: 841

Answers (2)

creativetechnologist
creativetechnologist

Reputation: 1462

You are getting the error because the route isn't working, what the code shows is that the url, /users/my_new_action, is trying to find a user id of '/users/my_new_action' for the default 'show' action.

I would replace the routes code you provided to something like (please not this is not tested code)...

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

Upvotes: 0

deviousdodo
deviousdodo

Reputation: 9172

The title is misleading, you've put <controller> because you're probably seeing User, but it most likely refers to a model you're trying to build or use. Because you've not pasted more code I cannot say exactly where's the issue, but look for a reference to an id you are using in the code - that's most likely null.

Upvotes: 2

Related Questions