1

I'm slightly lost in Rails 3.2. I've used to create skinny controllers in Padrino using methods like this:

 15   post :task, :provides => :js do
 16     result = execute(params)
 17     render "home/task"
 18   end

Some methods does not exactly interacts with a model. In Rails 3.2, I cannot use helpers in controllers to make them clean (like Rails 2.x or Padrino). I've created a few methods like this one:

 10   def show                                                                                                                                               
 11     @server = server_details                                                                                                                             
 12     respond_with(@server) if request_match_server_address?                                                                                               
 13   end     

But moved code from ServersController to ApplicationController, assuming it will be temporarily, and now application_controller is ugly and biggger (sure with three g's).

How can I make my controllers beauty? Where is the right place to put methods like server_details?

Ruy Rocha
  • 894
  • 8
  • 8

2 Answers2

1

in routes.rb

post "/task", to: "tasks#show"

in tasks_controller.rb

respond_to :js
def show
  @result = execute(params)
  respond_with @result
end

this will automatically use the view tasks/show.js

good luck

montrealmike
  • 11,433
  • 10
  • 64
  • 86
0

montrealmike's answer is how you would translate your Padrino to Rails 3.

But to answer the question you asked (with the "?"),
you can use helpers in Rails 3, as there just normal ruby modules.

You just need to include them:

include ServerHelper
def show
  @server = server_details
  respond_with(@server) if request_match_server_address?
end

where you have app/helpers/server_helper.rb

module ServerHelper do
  def server_details
    ...
  end
  def request_match_server_address?
    ...
  end
end

Edit: You can also include them in the ApplicationController and they will be inherited like all other good little classes.

complistic
  • 2,610
  • 1
  • 29
  • 37