Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Logic in controllers?

I am always reading about keeping Controllers thin and doing all logic in models. While this makes senses to me for interacting with databases, what about situations where there is no need to for database interactions?

I have a fairly complex module in my app that interact with several different third party APIs. I use ajax calls to my controller, where all the data is gathered from the APIs and then organized. Then it is displayed via the corresponding .js.erb or .html.erb files.

Is this the proper way to handle this kind of situation? I'm new to rails and don't want to get into habit of doing things wrong.

like image 782
Patm Avatar asked Apr 22 '11 19:04

Patm


People also ask

Where do I put business logic rails?

You can put business logic anywhere you want (even in views! though that's a bad idea). I'd say if the logic is tied to a real-world object, then put it on the model. Otherwise, use the controller.

What is controller callback in rails?

Abstract Controller Callbacks Abstract Controller provides hooks during the life cycle of a controller action. Callbacks allow you to trigger logic during this cycle. Available callbacks are: after_action. append_after_action.

What are rails concerns?

A Rails Concern is a module that extends the ActiveSupport::Concern module. Concerns allow us to include modules with methods (both instance and class) and constants into a class so that the including class can use them. A concern provides two blocks: included.

How can you tell Rails to render without a layout?

By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.


1 Answers

Models are not just for dealing with database, but for working with data in principle.

As far as we don't know what situations you mean I can just present some situations.

Ajax call for big Math calculating. It is not touching database and even it can be calculating in tableless model.

# in your controller
def calculating
  Calculator.get_integral_log_and_furie params[:data]
end
# in your model
class Calculator
  def self.get_integral_log_and_furie(data)
    ... # multi line code
  end
end

So you can see that you can calculate it right in your controller, but it should be calculated in your model, so it is reusable and clean solution.

Another example is using some virtual attributes. Names. You can store first, second and third name in saparate columns, so you need to join it. You can create privae method in controler, but of course it is bad idea.

class User < AR::Base
  def full_name
    [first_name, second_name, third_name].compact.join(" ")
  end
end

So you can call it everywhere in your project:

@user.full_name
# Peter Jhonson, or mu is too short

And so on and so on

like image 149
fl00r Avatar answered Oct 15 '22 01:10

fl00r