0

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

  • Try running `rake routes`. This will show you all the routes that are available to you (but without the `_path` part, so you would see `my_foos` for example, instead of `my_foos_path`). Do you fee `foo_bar_my_foos` in there? My guess is it's probably `my_foos_foo_bar` that is the correct path you want, but double check in there. – MrDanA Jan 22 '12 at 16:53

2 Answers2

0

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.

Jef
  • 5,424
  • 26
  • 28
  • Yes I can see my route on the left side, like I said this works quite well if I use the singular definition for a resourceful route. If I use 'resource' in my route definition instead of 'resources' it works the way it should as indicated in the rails documentation. But if I use 'resources' to define my controller as a plural resource, this is where I'm having difficulty. – Ray Wong Jan 22 '12 at 17:17
  • Did you try with the singular form for my_foos : foo_bar_my_foo_path ? – Jef Jan 22 '12 at 17:30
  • I meant that as you declare foo_bar as an individual member of my_foos, I think you should actually use the singular form for my_foos (foo_bar_my_foo_path) even if my_foos is declared as a collection resource. – Jef Jan 22 '12 at 17:54
0

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.

ez.
  • 7,604
  • 5
  • 30
  • 29
  • Thanks for your response, but the passing of the id is not the issue. I did try your suggestion and passed an id value to the helper, like so: foo_bar_my_foos_path(12) just to see what would happen, I get the same error. Fundamentally there appears to be a different convention used between the helper methods generated for a singular resource ( resource :my_foos ) vs the plural version of the resource ( resources :my_foos ), maybe a bug in rails 3? – Ray Wong Jan 22 '12 at 17:38