Rails Model, View, Controller, and Helper: what goes where?

后端 未结 10 902
不思量自难忘°
不思量自难忘° 2020-11-30 15:59

In Ruby on Rails Development (or MVC in general), what quick rule should I follow as to where to put logic.

Please answer in the affirmative - With Do put this h

相关标签:
10条回答
  • One thing that helps separate properly is avoiding the "pass local variables from controller to view" anti-pattern. Instead of this:

    # app/controllers/foos_controller.rb:
    class FoosController < ApplicationController
    
      def show
        @foo = Foo.find(...)
      end
    
    end
    
    #app/views/foos/show.html.erb:
    ...
    <%= @foo.bar %>
    ...
    

    Try moving it to a getter that is available as a helper method:

    # app/controllers/foos_controller.rb:
    class FoosController < ApplicationController
    
      helper_method :foo
    
      def show
      end
    
      protected
    
      def foo
        @foo ||= Foo.find(...)
      end
    
    end
    
    #app/views/foos/show.html.erb:
    ...
    <%= foo.bar %>
    ...
    

    This makes it easier to modify what gets put in "@foo" and how it is used. It increases separation between controller and view without making them any more complicated.

    0 讨论(0)
  • 2020-11-30 16:43

    Testing, Testing ... Put as much logic as possible in the model and then you will be able to test it properly. Unit tests test the data and the way it is formed by testing the model, and functional tests test the way it is routed or controlled by testing the controllers, so it follows that you can't test the integrity of the data unless it is in the model.

    j

    0 讨论(0)
  • 2020-11-30 16:44

    Do put stuff related to authorization/access control in the controller.

    Models are all about your data. Validation, Relationships, CRUD, Business Logic

    Views are about showing your data. Display and getting input only.

    Controllers are about controlling what data goes from your model to your view (and which view) and from your view to your model. Controllers can also exist without models.

    I like to think of the controller as a security guard/receptionist who directs you the customer(request) to the appropriate counter where you ask a teller (view) a question. The teller (view) then goes and gets the answer from a manager (model), who you never see. You the request then go back to the security guard/receptionist (controller) and wait until you are directed to go another teller (view) who tells you the answer the manager (model) told them in response to the other teller's (view) question.

    Likewise if you want to tell the teller (view) something then largely the same thing happens except the second teller will tell you whether the manager accepted your information. It is also possible that the security guard/receptionist (controller) may have told you to take a hike since you were not authorized to tell the manager that information.

    So to extend the metaphor, in my stereotyped and unrealistic world, tellers (views) are pretty but empty-headed and often believe anything you tell them, security guard/receptionists are minimally polite but are not very knowledgeable but they know where people should and shouldn't go and managers are really ugly and mean but know everything and can tell what is true and what isn't.

    0 讨论(0)
  • 2020-11-30 16:50

    To add to pauliephonic's answer:

    Helper: functions to make creating the view easier. For example, if you're always iterating over a list of widgets to display their price, put it into a helper (along with a partial for the actual display). Or if you have a piece of RJS that you don't want cluttering up the view, put it into a helper.

    0 讨论(0)
  • 2020-11-30 16:52

    MVC

    Controller: Put code here that has to do with working out what a user wants, and deciding what to give them, working out whether they are logged in, whether they should see certain data, etc. In the end, the controller looks at requests and works out what data (Models) to show and what Views to render. If you are in doubt about whether code should go in the controller, then it probably shouldn't. Keep your controllers skinny.

    View: The view should only contain the minimum code to display your data (Model), it shouldn't do lots of processing or calculating, it should be displaying data calculated (or summarized) by the Model, or generated from the Controller. If your View really needs to do processing that can't be done by the Model or Controller, put the code in a Helper. Lots of Ruby code in a View makes the pages markup hard to read.

    Model: Your model should be where all your code that relates to your data (the entities that make up your site e.g. Users, Post, Accounts, Friends etc.) lives. If code needs to save, update or summarise data related to your entities, put it here. It will be re-usable across your Views and Controllers.

    0 讨论(0)
  • 2020-11-30 16:55

    The MVC pattern is really only concerned with UI and nothing else. You shouldn't put any complex business logic in the controller as it controls the view but not the logic. The Controller should concern itself with selecting the proper view and delegate more complex stuff to the domain model (Model) or the business layer.

    Domain Driven Design has a concept of Services which is a place you stick logic which needs to orchestrate a number of various types of objects which generally means logic which doesn't naturally belong on a Model class.

    I generally think of the Service layer as the API of my applications. My Services layers usually map pretty closely to the requirements of the application I'm creating thus the Service layer acts as a simplification of the more complex interactions found in the lower levels of my app, i.e. you could accomplish the same goal bypassing the Service layers but you'd have to pull a lot more levers to make it work.

    Note that I'm not talking about Rails here I'm talking about a general architectural style which addresses your particular problem.

    0 讨论(0)
提交回复
热议问题