0

I am learning Ruby on Rails. I made a simple link like this:

<%= link_to "Alex Link", alexes_path(@alex) %>

then I routed it in routes.rb like this:

  resources :alexes

  get "home/index"

then I am a bit unclear, but I think it goes to this part of the controller:

  def index
    #@alexes = Alex.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @alexes }
    end
  end

Am I correct that it goes to this part of the controller?

Then nothing much happens and it goes to the next page which is index.html.rb under views\alexes

So what I am wondering is - if I needed to do some business logic, would I write that in the controller snippet? Where inside the snippet? An example would be nice to take a look.

Also, I would like to connect to a MongoDb database. Would I also write that in the middle of the controller?

Thanks!

Genadinik
  • 18,153
  • 63
  • 185
  • 284
  • What is "alex"? A Static Page? - I'd also suggest to read: http://edgeguides.rubyonrails.org/getting_started.html if you don't know these guides allready. - Just for clarification. Normally try to avoid putting business logic into controllers. – Deradon Apr 12 '12 at 23:20
  • @Deradon Alex is my name :) Its a test page that is very static now, but I plan to change it with more complex stuff as I learn how to do that :) – Genadinik Apr 12 '12 at 23:21
  • @Deradon but if not the controllers, where? It seems that right after it hits the controller, it goes to the next view. That is confusing. – Genadinik Apr 12 '12 at 23:22
  • Ah ok. So we could change "Alex" with User. So you could just create a User Model and put all business logic into it. If you're experimenting try `rails g scaffold user name:string`. This will create a model, its corresponding controller and some routes. So you'll see how rails is basically working. – Deradon Apr 12 '12 at 23:23
  • @Deradon just tried that. It created lots of files. So i am not clear, I'd write the business logic in app/models/user.rb ? – Genadinik Apr 12 '12 at 23:28
  • @Deradon or in case of the "alex" call, it would be alexes_controller.rb ? – Genadinik Apr 12 '12 at 23:29
  • "Alex" is more like an instance of a "User"-Model. But you should read the link I provided above or the one "andrunix" provided below to get a basic understanding. Found older Stackoferflow Question too: http://stackoverflow.com/questions/4558800/where-does-business-logic-goes-in-rails – Deradon Apr 12 '12 at 23:30
  • @Deradon btw, am I mis-reading or you are saying the biz logic goes in the model and andruinix is saying the biz logic goes into the controller? :) – Genadinik Apr 12 '12 at 23:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10027/discussion-between-deradon-and-genadinik) – Deradon Apr 12 '12 at 23:34

1 Answers1

0

Yes, controllers are for business logic. Models represent the data, Views represent the display (usually a web page), and Controllers are precisely where business logic goes.

Check this site for an excellent tutorial: http://ruby.railstutorial.org/

andrunix
  • 1,704
  • 1
  • 14
  • 23
  • thanks, but notice above, that is opposite of that Deradon was saying. Or am I misunderstanding you guys? – Genadinik Apr 12 '12 at 23:30
  • 2
    Fat models, skinny controllers. That's best practice. Try to not put everything in the controller. E.g.: if you have a method like `backup_user(user)` in your controller, you should move it to `User#backup` and just call this in your controller. - Or just from the RailsApi: "The Model layer[..-] encapsulates the business logic" – Deradon Apr 12 '12 at 23:32