Hi, Is there a way to call helper methods in a controller? I want to do something like this in my controller Class MyController < Action.... def my_method string = link_to "some_url", :controller => "home", :action => "command" end end link_to is an ActionView helper method and it seems that I couldn't access the method in the controller even if I used the complete namespace... ActionView::Helpers::UrlHelpers::link_to I've also done some testing in irb and it says the method is undefined.. >> ActionView::Helpers::UrlHelper => ActionView::Helpers::UrlHelper >> ActionView::Helpers::UrlHelper::link_to NoMethodError: undefined method `link_to' for ActionView::Helpers::UrlHelper:Module from (irb):59 Any clues? Thanks, Mac
on 2006-05-21 12:10
on 2006-05-22 04:42
Hi Mac, big mac <bigmac928@...> writes: > > Hi, > Is there a way to call helper methods in a controller? > I want to do something like this in my controller > Class MyController < Action.... > def my_method > string = link_to "some_url", :controller => "home", :action => "command" > end > end > link_to is an ActionView helper method and it seems that I couldn't Using render :inline you can use the normal <% %> tags which can contain helper methods. Try something along the lines of render :inline => "<%= link_to 'some_url', :controller => 'home', :action => 'command' %>" Of course you might want to render more than the link, but that's what views are for. Bob PS sorry if this is not what you were hoping for.
on 2006-05-22 06:51
Hi Mac, Helpers are Ruby Modules so you can include them in controllers (or any object) like so (though I'm not sure if someone's going to smack me for suggesting this): class SomeController < ApplicationController include ActionView::Helpers::NumberHelper def index # you can use number_to_currency here, for example. end end I use this to format error messages my models generate. I.e. I want to say "You can't afford this, you only have $30.00 and the item costs $32.00" rather than 30. and 32.0. I imagine it could get a bit out of hand (hence my smack comment above). Rails is supposed to guide you to develop well encapsulated applications, so use this wisely. Ryan.
on 2006-05-22 09:22
> Helpers are Ruby Modules so you can include them in controllers (or any > object) like so (though I'm not sure if someone's going to smack me for > suggesting this): (no smacking, just thinking ;) i didn't try, but isn't there the caveat of having all the module public methods exposed as valid controller actions in that case ? (I'm not sure weither the module methods will be exposed as action, but it deserves a test!) hth
on 2006-05-22 09:37
Oh, that's a very interesting point... The hepler methods are infact exposed when you simply include it! We could do this, though: include ApplicationHelper for m in ApplicationHelper.instance_methods hide_action m end Thus using ActionController's hide_action to hide all the included methods... Or some meta-thingy: class ActionController::Base def self.include_helper_in_controller(mod) include mod for m in mod.instance_methods hide_action m end end end And then just go: class MyController < ApplicationController include_helper_in_controller ApplicationHelper end I typically only include helpers in my models (and rarely at that) to help format error messages :) > (no smacking, just thinking ;) I suppose thinking is better than smacking in this case! (though I'd argue that depends on the company). -RYan.