7

In the interest of keeping my Controller's skinny, I put a headline method in a Model.

# My Model
class Property
  def headline 
    "#{name} | #{address}"
  end
end

(Then, Property Controller for context...)

# My Controller    
class PropertyController
  def show
    @property = Property.find(params[:id]) 
  end
end

Question: As far as MVC is concerned, is it okay to call that Model method from my View, like so?

# My View
<h1><%= @property.headline %></h1>
  • 3
    In certain MVC variants, the view gets notified of changes in the model via the controller: the view registers as a listener on model-changed notifications, but the view never calls methods of the model. In another variant, the view queries (calls getter methods of) the model directly to get the data to be displayed. However, the view should only be able to query the model, not to change its contents. As far as I understand, your example is OK since the view is only querying the model via @property.headline. – Giorgio Jun 8 '13 at 18:42
  • @Giorgio: Could you turn that comment into an answer? – Bart van Ingen Schenau Jun 9 '13 at 11:41
3

In certain MVC variants, the view gets notified of changes in the model via the controller: the view registers as a listener on model-changed notifications, but the view never calls methods of the model (see e.g. the diagram in the wikipedia article).

In another variant, the view queries (calls getter methods of) the model directly to get the data to be displayed. However, the view should only be able to query the model, not to change its contents. To my knowledge, this is the JSP approach: the view (a JSP page) can contain queries that get data from the model to be rendered with the rest of the page.

As far as I understand, your example follows the second approach and it is therefore OK since the view is only querying the model via @property.headline.

1

The whole point of MVC, or any other programming paradigm, is to get good separation of concerns. Slavish adherence to your paradigm's principles should be insisted upon only when you can specify a greater benefit to doing so than not.

If you're willing to accept the need to possibly edit your View when a new headline rule comes along, skipping an empty property on the PropertyController is probably fine. Especially if your View already has an included dependency to the model.

On the other hand, if your View has no other direct reference to the model, you'd probably be better off just defining the "empty" parameter on the PropertyController, so you can change or move the logic without having to worry about the View.

(If you have a lot of these, you may want to explore a generic get function on the controller, and have the view call that instead of direct property or method references.)

0

In my opinion it's a good solution for observer pattern (your model can be observable object which will notify its observers that data has changed). For example model (ex. Human) has changed its property (ex. weight) so he notifies its registered observers about it (ex. views). After receiving that information all observers (ex. view) can change its data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.