Resources, Modules, and Nesting (URL Helper errors)

Hi,

I am trying to create REST routes non-standard setup. The rest url
helpers give an error when I try to use a module controller for a
nested resource.

Works
map.resources :bar, :controller => ‘module/bar’

Works
map.resources :foos do |foo|
foo.resources :bar
end

Doesn’t work (url helpers broken)
map.resources :foos do |foo|
foo.resources :bar, :controller => ‘module/bar’
end

The URL helper error is…
ActionView::TemplateError (bar_url failed to generate from
{:action=>“index”, :controller=>“module/bar”, :foo_id=>“1”}, expected:
{:action=>“index”, :controller=>“module/bar”}, diff: {:foo_id=>“1”})

Anybody have any suggestions on what is wrong? Is it just a limitation
or feature of rest routes?

Thanks,
Stephanie

Hey,

could be a pluralization issue:

map.resources :foos do |foo|
map.resources :bars, :controller => ‘module/bar’
end

note I said :bars not :bar

HTH,
Trevor

Hi Trever,

Thanks for the reply. Sorry for the typo. My email should say bars (a
typo in translation to a question).

map.resources :bars, :controller => 'module/bars

My code actually did have the right pluralization. I copied the code
out of the nesting and tested it unnested. All of the helpers worked
with a module resource it isn’t nested.

-Stephanie

Hey Stephanie,

interesting…

to be honest I’m not even trying it out with a controller inside a
module because the error you’re getting seems to be coming from the
url_helpers, which don’t care about controllers as such.

So, at the risk of sending you on a wild goose chase, I’ll make a guess:

I’m betting you’ve got “map.resources :bars” in more than one place
in your routes.rb - am I right?

Trevor

Thanks for trying to track this down. I have no other references
to :bars in the routes. I also do not have /:controller/:action/:id as
a route.

However, in my controllers folder I do have a

app/controllers/bars_controller.rb
app/controllers/foo/bars_controller.rb (this is subclassed from
controllers/bars_controller.rb)

Could this be causing the problem?

-Stephanie

Stephanie,

I’m confident that the names of your controllers isn’t the source of
the problem - I can use utterly bogus controller names (and modules)
in map.resources calls and it has no effect on url helpers.

Is this something as simple as that you’re not actually passing all
the required information to the url helpers for your nested
resource? As in:

bar_url(‘a_foo’, ‘a_bar’) #=> http//host.com/foos/a_foo/bars/a_bar

Trevor

I tried changing the super controller to another name. That way the
only bars controller would be foo/bars_controller.rb. This still gave
me the same URL helper problems as described above.

I am back to suspecting is modules + nested resources is the
problem???

-Stephanie

Hi Stephanie,

Have found a solution to your problem? I have the same problem as well.
My routing looks as follows;

map.resources :pictures, :controller => ‘user_pictures’, :path_prefix =>
‘/:user_uid’, :name_prefix => ‘user_’, :requirements => { :user_uid =>
/shafeer/ } do |picture|
picture.resources :exhibitions, :controller =>
‘picture_exhibitions’, :name_prefix => ‘user_picture_’
end

and then;

map.resource :myapp, :controller => ‘myapp/index’ do |myapp|
myapp.resources :pictures, :controller => ‘myapp/pictures’,
:name_prefix => ‘myapp_’ do |picture|
picture.resources :exhibitions, :controller =>
‘myapp/picture_exhibitions’, :name_prefix => ‘myapp_picture_’
end
end

The helper method myapp_pictures_path produces the right path #=>
http://host.com/myapp/pictures.

But the problem I have is with the controller, the routing chooses the
wrong controller, that is, /app/controllers/user_pictures_controller.rb
instead of
/app/controllers/myapp/pictures_controller.rb

Any idea why is that?

Thanks and Regards,
Shafeer.