Crashalot
Crashalot

Reputation: 34503

How to share code between model and controller in Rails?

Both my Rails model and controller code need to write files to the file system.

I would like to consolidate the logic into one method.

What's the best way to share this method across models and controllers?

Thanks!

Upvotes: 2

Views: 2292

Answers (2)

jdl
jdl

Reputation: 17790

If you really need to do this, you could place a module in /lib and include it where needed.

However, if possible you should have your model take care of it. If you can supply some more details, it would be easier to steer you in the correct direction.

Upvotes: 2

Matthew Vines
Matthew Vines

Reputation: 27561

I think the controller would defer the actual execution of writing a file to the file system to the model. While the controller is allowed to decide when to execute that code, it should not be responsible for it's implementation, So that code should really only be in the Model.

Upvotes: 2

Related Questions