Topic: How far it's good to use helpers into controllers

Hi all,
We are follow coding standards while writing code.
I want to know some facts about controller coding standards.Generally we will use helpers for views.
We can use helpers in controllers.But as perstandards can we incldue in controllers if its not corerct.Can you tell the reasons.

Re: How far it's good to use helpers into controllers

I'm having difficulty understanding your question. Are you asking why it's not good practice to use helpers in your controllers?

Typically what I do (and someone can chime in here if they have a more clever idea) is define helper methods for controllers beneath a protected section within those controller definitions. So, if you have a procedure for finding a specific blog record (for editing, viewing or whatever) you could do something like this

class BlogPostController < ApplicationController
    ...
    def edit
        find_post_by_name_or_id
        ... # do stuff
    end

    def show
        find_post_by_name_or_id
        ... # do stuff
    end

    protected

    def find_post_by_name_or_id
        if params[:id]
            @blog_post = BlogPost.find(params[:id])
        elsif params[:name]
            @blog_post = BlogPost.find(params[:name])
        end
    end
end

This way, the defined method (find_post_by_name_or_id) couldn't ever get called as an action, but it would be available to any controller instance in charge of responding to a request.

If you need a similar controller helper that can be used in any controller (not just a specific controller) you can put those in the protected section of your application_controller.rb, since each of your controllers will inherit from that.

The point of the helpers in the helpers directory is for use as view helpers. That's basically all there is to answer the question of why you wouldn't want to use them in your controllers. If putting model logic in your models and writing view helpers isn't enough to keep your controllers clean, you can created modules that you put in the lib directory and require/include these in your controllers that need those helper methods. Or if it's something you think you'll use a lot, you could (and should) write a gem for that stuff.

Hope this helps.

Chris