NameError in my_foos#index undefined local variable or method 'foo_bar_my_foos_path'

464 Views Asked by At

I'm having a problem with understanding how RESTFUL resources works in Rails 3. If I have in my rails routes.rb file:

resources :my_foos do
   member do
      get 'foo_bar'
   end
end

I have a controller file my_foos_controller.rb

class MyFoosController < ApplicationController
   def index
   end

   def foo_bar
   end
end

I have a view file test.html.erb

<%= foo_bar_my_foos_path %>

When I try to display my test.html.erb file, I get an error: as in the title.

Everything I've described to you works if I'm dealing with a resource ( singular ) but not with the plural resources. Am I missing something in the convention?

Thanks

2

There are 2 best solutions below

3
On

Use rake routes from the command line to display your routes. The leftmost part of each route is the name of the route. Append _path to get it working.

1
On

if foo_bar is declared as a member action, then, by rails convention, you will need to supply the corresponding model resource id when you use named routes. something like this should work

foo_bar_my_foos_path(@my_foos) # or @my_foos.id

If you do not want to supply the id, use collection in your routes instead.