big mac
2006-May-21 10:08 UTC
[Rails] Is there a way to call helper methods in a controller?
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060521/34067e03/attachment.html
Bob Firth
2006-May-22 02:40 UTC
[Rails] Re: Is there a way to call helper methods in a controller?
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''tUsing 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.
Ryan Allen
2006-May-22 04:52 UTC
[Rails] Re: Is there a way to call helper methods in a controller?
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.
--
Posted via http://www.ruby-forum.com/.
big mac
2006-May-22 05:00 UTC
[Rails] Re: Re: Is there a way to call helper methods in a controller?
Hi Bob, Since I wanted it in a string, I''ve used it this way: string = render_to_string :inline => "<%= link_to ''some_url'', :controller => ''home'', :action => ''command'' %>" Does the job. Thanks! Mac ------------------------------> > Message: 7 > Date: Mon, 22 May 2006 02:34:51 +0000 (UTC) > From: Bob Firth <r.firth@ridley.unimelb.edu.au> > Subject: [Rails] Re: Is there a way to call helper methods in a > controller? > To: rails@lists.rubyonrails.org > Message-ID: <loom.20060522T042945-322@post.gmane.org> > Content-Type: text/plain; charset=us-ascii > > 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. > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060522/bb27fc6a/attachment.html
big mac
2006-May-22 05:45 UTC
[Rails] Re: Re: Is there a way to call helper methods in a controller?
Hi Ryan, This is exactly what I''m looking for! Thanks, Mac> > Date: Mon, 22 May 2006 06:51:45 +0200 > From: Ryan Allen <ruby-forum@badfont.com> > Subject: [Rails] Re: Is there a way to call helper methods in a > controller? > To: rails@lists.rubyonrails.org > Message-ID: <9f1ff028e7c798e63fb8c0e86d1eb74a@ruby-forum.com> > Content-Type: text/plain; charset=utf-8 > > 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. > > -- > Posted via http://www.ruby-forum.com/. > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060522/10a0acf9/attachment.html
Thibaut Barrère
2006-May-22 07:19 UTC
[Rails] Re: Is there a way to call helper methods in a controller?
> 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060522/d57dde49/attachment.html
Ryan Allen
2006-May-22 07:34 UTC
[Rails] Re: Is there a way to call helper methods in a controller?
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.
On 22/05/2006, at 5:19 PM, Thibaut Barr?re wrote:
> > 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
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails