Problem with overriding URL helper methods

Hi,

I’ve got troubles overriding some Rails generated URL helpers…
My site allows registered users to create some kind of diagrams online,
and then to browse all created diagrams, etc.
I’ve got a route to access a diagram created by a user:
map.diagram ‘:owner_id/:id’, :controller => ‘diagrams’, :action =>
‘show’

When I want to have a link or redirect to a diagram, I’m currently doing
this:
redirect_to diagram_path(diagram.owner, diagram) – so that :owner_id
and :id are correctly set

I want to do something so that diagram_path(diagram) would generate the
correct URL with diagram’s owner id and diagram’s id.

What I have tried to do is to rename my route:
map.full_diagram ‘:owner_id/:id’, :controller => ‘diagrams’, :action =>
‘show’

and then in DiagramsHelper, to override diagram_path and diagram_url
like this:
def diagram_path(diagram, *args)
full_diagram_path(diagram.owner, diagram, *args)
end

It’s working well, except for one thing: the method is not available in
my controllers. Though I’ve got “helpers :all” in ApplicationController.
So, in a view, diagram_path(diagram) would give “/olance/27”, whereas a
redirect_to diagram_path(diagram) in a controller would redirect to
“/diagrams/27”.

If I copy my overriden methods in application_controller.rb, then it
does work. So I really don’t get it, why would my helpers not be
included?

Is there a better way to do what I’m trying to achieve? I might have
missed something in the routes mechanisms… nested routes maybe?
I’m using Rails 2.3.8 by the way.

Thanks a lot,
Olivier

Olivier L. wrote:

Hi,

I’ve got troubles overriding some Rails generated URL helpers…
My site allows registered users to create some kind of diagrams online,
and then to browse all created diagrams, etc.
I’ve got a route to access a diagram created by a user:
map.diagram ‘:owner_id/:id’, :controller => ‘diagrams’, :action =>
‘show’

When I want to have a link or redirect to a diagram, I’m currently doing
this:
redirect_to diagram_path(diagram.owner, diagram) – so that :owner_id
and :id are correctly set

I want to do something so that diagram_path(diagram) would generate the
correct URL with diagram’s owner id and diagram’s id.

What I have tried to do is to rename my route:
map.full_diagram ‘:owner_id/:id’, :controller => ‘diagrams’, :action =>
‘show’

and then in DiagramsHelper, to override diagram_path and diagram_url
like this:
def diagram_path(diagram, *args)
full_diagram_path(diagram.owner, diagram, *args)
end

It’s working well, except for one thing: the method is not available in
my controllers. Though I’ve got “helpers :all” in ApplicationController.
So, in a view, diagram_path(diagram) would give “/olance/27”, whereas a
redirect_to diagram_path(diagram) in a controller would redirect to
“/diagrams/27”.

If I copy my overriden methods in application_controller.rb, then it
does work. So I really don’t get it, why would my helpers not be
included?

Is there a better way to do what I’m trying to achieve? I might have
missed something in the routes mechanisms… nested routes maybe?
I’m using Rails 2.3.8 by the way.

Thanks a lot,
Olivier

Looks like nested routes would be a better approach for this

You’d need
map.resources :owner, :has_many => diagrams

This would create this type of restful
route owner_diagrams_path
route_owner_diagram_path(@owner, @diagram)
etc
Once you have the route.rb file in place, you can run rake routes from
rails_root to see all available routes

Luis S. wrote:

Looks like nested routes would be a better approach for this

You’d need
map.resources :owner, :has_many => diagrams

This would create this type of restful
route owner_diagrams_path
route_owner_diagram_path(@owner, @diagram)
etc
Once you have the route.rb file in place, you can run rake routes from
rails_root to see all available routes

Thanks for your reply!
I’ll certainly try going this way… however, do you have any idea about
this overriding thing? I’d like to understand why my views can use my
overriden methods but not my controllers… :\

Luis S. wrote:

Olivier L. wrote:

Luis S. wrote:

Looks like nested routes would be a better approach for this

You’d need
map.resources :owner, :has_many => diagrams

This would create this type of restful
route owner_diagrams_path
route_owner_diagram_path(@owner, @diagram)
etc
Once you have the route.rb file in place, you can run rake routes from
rails_root to see all available routes

Thanks for your reply!
I’ll certainly try going this way… however, do you have any idea about
this overriding thing? I’d like to understand why my views can use my
overriden methods but not my controllers… :\

Helpers are made for views and not controllers. However, you can still
use them in your controller. Not sure whats up in your case but you can
do a quick google search for rails using helpers in controllers

I partly agree with that… Helpers are made for views indeed, but who’s
ever been doing a controller without any URL helper?
That’s maybe why my overridden ones aren’t in my controllers, maybe
they’re not Helpers as other view helpers? I’ll try to figure that
out…

Another thing now… I’m using acts_as_commentable and I have a comments
controller that can create a comment and associate it to any commendable
model.
I’m using polymorphic_path to redirect to the model show page once the
comment is added. How can I still do this with a nested route, which
will require owner plus model’s id, when any other model pah would just
require the model’s id?!
(that’s why I came to the override at first)

Thanks again!
Olivier

Olivier L. wrote:

Luis S. wrote:

Looks like nested routes would be a better approach for this

You’d need
map.resources :owner, :has_many => diagrams

This would create this type of restful
route owner_diagrams_path
route_owner_diagram_path(@owner, @diagram)
etc
Once you have the route.rb file in place, you can run rake routes from
rails_root to see all available routes

Thanks for your reply!
I’ll certainly try going this way… however, do you have any idea about
this overriding thing? I’d like to understand why my views can use my
overriden methods but not my controllers… :\

Helpers are made for views and not controllers. However, you can still
use them in your controller. Not sure whats up in your case but you can
do a quick google search for rails using helpers in controllers