Rails: controller helpers - ruby-on-rails

If I have a methods that I want to use in several controllers, to keep my code DRY.
What is the rails way of doing this?

If you want to have your code reusable, I think application_controller is the best place to do so. Create any method you think it's reusable in multiple controllers in application_controller and call them in any controller you want.

Related

Make partials in a controller

I'm building a ruby on rails app and I have rather big controllers.
Now I have the same actions to be done on a create action and a delete action so I was wandering if it's possible to make this DRY. Or is this only possible with views?
There are a few ways you can achieve this:
Move all model related code to your models and call model actions on models in your controller. This is a great way to clean up your controller especially if you are querying the model for generic things.
Create private actions in your controller to reduce duplication of code.
You can create methods inside a module and import the module into your controller. This should reduce the size of your controller as well as making it DRY (you could even reuse that module in different controllers).
module MyModule
def my_method
end
end
# in controller
include MyModule
You can only create partials in view.
Code repeatation in controller can be made DRY by writing the similar part of code as a method in Model and calling it in as many actions in controller.
In rails4 had a folder which called concern, you can use concern to split your code in controller.
You can refer about concern at: concern

When to add method to application_controller.rb

My shopping site has a header that's common across all pages.
I have added a shopping cart icon in the header, with a number that updates based on the contents in the current cart.
Because this icon requires a definition for current_cart, I now need to add this to every controller action. Is this unwise/unsafe? I'm new to rails, and don't quite understand the security repercussions of adding methods to applications.rb
Also, is the best way to do this to add this once to application_controller.rb, or to add it separately to each relevant controller action?
Thanks in advance for the feedback!
Having DRY in mind it's definitely better to add this method once, one level up in yours controller inheritance tree. If all controllers need that method then application_controller.rb is a good place. If just some of them then you may consider to create controller which inherits from ApplicationController, store that method in it, and all controllers that requiers that method should inherits form it.
All of your controllers inherit from the application controller. This means that any method you define in the application controller is available in all of your other controllers.

Is It Okay to use helpers in views?

Simple question about best-practice. I'm using Kohana... is it okay to use helpers in views? For example, to use URL::site(). I could pass it from controller, you know. I assume it's okay, because there are helpers like HTML that is meant to be used in views, right?
The way you're currently doing it is ok, altough the whole practice of having any logics in views is questionable, but it's how Kohana is currently recommending.
When you get to use ViewModel pattern (with Kostache?), you'll separate all logics from templates. Until then, it's ok to use methods that don't do anything that should be done in the controller / model (echo, conditions and loops are "considered allowed").

call a helper from another view

I know that if I want to call a helper of another controller, I can do something like:
helper :other_controllers
But I was wondering why I can't do something like OtherControllersHelper.method inside the view?
Due to the way that Rails loads your modules, you cannot do this without modification.
Rails includes the associated helper models into the ActionView::Base instance used to render a template. ActionController::Helpers#helper (used in the example above) adds more helper modules to the list of those to be included. The helper methods that are used in views are written as instance methods. Modules in Ruby do not provide any good ways of getting at instance methods without using a constructor. Which is one of the big things that separates modules from classes.
To access your helpers from another controller with just OtherControllersHelper.method, you will need to redefine method as a class method. However, redefining those methods as class methods would make them inaccessible from your views.
You could duplicate all instance methods in your helpers as class methods, but that's definitely not a better solution that adding helper :other_controllers. There are ways to define wrappers pragmatically, but again, it's not the best way to handle the situation.
If you've got a lot of helpers that are likely to be used in multiple controllers/views maybe you're better off putting them somewhere else. Somewhere like app/helpers/application_helper.rb. Or another helper module that could be loaded only in the controllers that need it.

Use helper methods within a controller

I defined a helper abc() in annotations_helper.rb. What do I have to do such that I can use this method in annotations_controller.rb?
In general helpers are supposed to be "view helpers" and not called from controllers.
You probably want to put something like that in application_controller.rb
This is usually not a good practice to use a helper in your controllers. You should try to move the logic inside a model or if the logic is too generic, you should move that to lib/some_lib.rb and include that in your model to use.
However check out this blog post if you really want to do this. Don't forget to read the comments.

Resources