Forum: Ruby on Rails "Helper" for 2 controllers?

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.
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-04-23 21:47
Is there a DRY way to have a helper method for 2 different controllers?

For example:
@user.create_points_account(10)
@group.create_points_account(10)

and the helper method would look like this:

def create_points_account pts
  f = PointAccount.new(:points => pts)
  f.resource = self
  return false if !f.save
end
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-04-23 21:50
(Received via mailing list)
Place it in a module somewhere and have both controllers to include that
module.

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)



On Thu, Apr 23, 2009 at 4:47 PM, Heinz Strunk
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-04-23 22:03
I included it in the controller with helper :points and called it like
above but I get an error:
undefined method create_points_account

What could be the problem?

Maurício Linhares wrote:
> Place it in a module somewhere and have both controllers to include that
> module.
>
> -
> Maur�cio Linhares
> http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
> (en)
>
>
>
> On Thu, Apr 23, 2009 at 4:47 PM, Heinz Strunk
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-04-23 22:08
(Received via mailing list)
I guess I did not made myself clear.

I said create a module somewhere (usually under the lib folder), like:

#lib/points.rb

module Points

 def create_points_account( pts )
  f = PointAccount.new(:points => pts)
  f.resource = self
  return false if !f.save
 end

end

And then at your controller:

class SomeController < ActionController::Base

  include Points

end

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)



On Thu, Apr 23, 2009 at 5:03 PM, Heinz Strunk
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-04-23 22:41
I changed it from helper :fund to include Fund and it's included now but
it doesn't really help cause there's still the error:
undefined method `create_fund' for #<User:0x5d52c6c>
This topic is locked and can not be replied to.