Use code form one controller in another

Hi all,

Can I use code from one controller in another controller? If so, how
do i do this?

Thanks
Stijn

On Thursday 22 February 2007, Tarscher wrote:

Can I use code from one controller in another controller? If so, how
do i do this?

You don’t. You extract the common code into a third piece. Either an
object that is instantiated in each of the controllers or a mixin
module that is included in each.

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/

Thanks for the reply,

Where in my rails structure do I put this common code directory wise?
Somewhere in the app director?

Is there some tutorial on this?

Stijn

Where in my rails structure do I put this common code directory wise?

Somewhere in the app director?
Is there some tutorial on this?

http://www.robbyonrails.com/articles/2007/02/09/extending-actioncontroller-part-two

-ed

On 2/22/07, Alex T. [email protected] wrote:

I don’t believe you can use the same code from a controller and in a
view - I’ve had to put a couple functions in both files. Though I may
be wrong.

You can.

class ApplicationController < ActionController::Base
helper_method :current_user, :logged_in?
end

This will make the ‘current_user’ and ‘logged_in?’ controller methods
available to all views.


Chris W.

Tarscher wrote:

Thanks for the reply,

Where in my rails structure do I put this common code directory wise?
Somewhere in the app director?

Is there some tutorial on this?

Stijn

There may be a more official way to do this, but what I’ve done is to
create a file ‘application.rb’ in the controller directory and put
shared functions in there. Works nice for validation or db queries etc.

I do the same for views / helpers by using a application_helper.rb file.

I don’t believe you can use the same code from a controller and in a
view - I’ve had to put a couple functions in both files. Though I may
be wrong.