2

I am new to Ruby on Rails, and I am not confident about my MVC logic.

I am proceeding in this way:

  • I have a model 'User'
  • In my controller, I set an instance variable called @users = User.all
  • In my views, I implement my logic like this : <%= @users.each ... %>

I was asking myself in I could bypass the controller step and write this in my views : <%= User.all.each ... %>

I'd like to use good practices in my project, is the second way acceptable?

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Syph3R
  • 77
  • 9

4 Answers4

1

in real life operations in controller more complexier then User.all.each for example

@cars = Car.scoped
@cars = @cars.includes(:body_type, :brand, :city, :drive, :engine_type, :model, :region, :transmission)
@cars = @cars.select(['`cars`.*','`stats`.recount']).joins('left outer join stats on (cars.model_id = stats.model_id and cars.year = stats.year)')
@cars = @cars.limit(15)

and mode and more other logic

in view you need only to render this object
in my example i use render partial in view

= render @cars

and in view/_car.html.haml

- for car in @cars
  .row-fluid.car
    = car.price
    = etc

so, if you whant to change view you changing view
if you whant to change behavior of collecting process you changing controller
if you whant to change behavior of object you changing model

the same things with bugs
all in strict order
the order is good and beautiful

antiqe
  • 1,125
  • 8
  • 17
  • Ok, understood. I would like to have another piece of advice. In case I have an instance variable in my controller, which is set in the 'new' action, I can use it in my view 'new.html.erb'. However, if the user faces a validation error, he is redirected to the 'create' action in my controller, where I have to set my instance variable again. It happens when I use my instance variable in the collection_select helper. Is there a way to avoid this duplication ? (This is the main reason why I tried to directly use models in my views...) – Syph3R Jun 28 '12 at 08:51
  • Cause in the RESTfull pattern, you can`t get response directli in view. You shout post paramters from new and get responce from create action. You have to do it because it is right way in RESTfull paradigma. – antiqe Jun 28 '12 at 09:06
  • in other case you can use models directly, but it is php-way not rails-way it is not strict rule, it is a recommendation to make life perfect – antiqe Jun 28 '12 at 09:18
  • Thank you for answering antiqe, your recommendation made my morning perfect, it is a good start ;) – Syph3R Jun 28 '12 at 10:02
0

Technically, you can, your User model is just a Ruby class and is accessible from your views. However, it is most definitely not a good practice.

Rather than going in lengths to talk about why this shouldn't be done, I'll just link to some articles/documentation for you and advice you to get a good feel for the reasoning behind MVC architecture first, before moving further into Rails:

Community
  • 1
  • 1
JeanMertz
  • 2,250
  • 2
  • 21
  • 26
0

The idea of interacting with the query interface of ActiveRecord only from within controller actions is maintainability. If you follow the approach you can roughly know what an application is supposed to do by looking at it's controllers. Also, if the logic would become more complex (e.g. you want to add authentication and/or authorization), that behavior would go into the controller.

In contrast, if you look at one of your views, it should be clear how the page looks. If there are calls to model classes all over the place, this is less obvious.

Perhaps, you can think of it like this:

  • Model: complex queries, validations, callbacks to serve your business logic
  • Controller: What is being shown/created/updated/etc. and who is allowed to do it.
  • View: How does my page look visually

If you want to change a certain aspect of your app, you should only have to touch the respecitve component responsible for the aspect.

moritz
  • 25,477
  • 3
  • 41
  • 36
0
  • Model: everything that do any kind of logical analysing and data maintaining
  • Controller: should just analyse request and deside what model should it ask for data in what way( All data? Only one field? ...also: am I admin or not ? )
  • View: just visual representation of the data that gets from controller

so yes it's possible to move everything to view (even database request), but it's not good idea, Ideally you should move it to controller

equivalent8
  • 13,754
  • 8
  • 81
  • 109