Forum: Ruby on Rails Is there a way to call helper methods in a controller?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1d623696d26ebebafecb8a2a2bc0f32e?d=identicon&s=25 big mac (Guest)
on 2006-05-21 12:10
(Received via mailing list)
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
3900093785084e4d58ec96aa699d1d03?d=identicon&s=25 Bob Firth (Guest)
on 2006-05-22 04:42
(Received via mailing list)
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.
9ffef62c0a3c095367140a3c08b15971?d=identicon&s=25 Ryan Allen (Guest)
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.
91eb330fb36d1e03c856574dfb77d2bc?d=identicon&s=25 Thibaut Barrère (thbar)
on 2006-05-22 09:22
(Received via mailing list)
> 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
Bcd8911eeed71ce2865d8670aecaf661?d=identicon&s=25 Ryan Allen (Guest)
on 2006-05-22 09:37
(Received via mailing list)
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.
This topic is locked and can not be replied to.