Topic: How to correct use MVC in rails?

Hi all!

My questions is about how using MVC in rails.
For example, we have two models: Event and Reply.
Event has_many Replies, Reply belongs_to Event.

EventsController:

  # View event:
  def show
    @item = Event.find(params[:id])
  end

View events/show.html.erb:

  ....
  <%= @item.title %>
  ....
  <% @item.replies.each do |reply| %>
    <%= reply.who %>
    <%= reply.message %>
  <% end %>

It is correct to call model's methods from view?
And what if I need to sort or filter replies?
Looks ugly: <% @item.replies.where('deleted = 0').order('date').each do |reply| %>

May be it's correct to fetch replies in controller?

  # View event:
  def show
    @item = Event.find(params[:id])
    @replies = Event.replies.where('deleted = 0').order('date')
  end

Or sorting and filtration must be in new model method?

  def show
    @item = Event.find(params[:id])
    @replies = Event.fetch_replies(params[:id]) # <- our new model class method
  end

That method must be class method, as I wrote above or instance method as below?

  
  def show
    @item = Event.find(params[:id])
    @replies = @item.fetch_replies # <- or instance method?
  end

   

Or, may be, it's must be method of Reply model?

  def show
    @item = Event.find(params[:id])
    @replies = Reply.find_by_event(@item) # with filtration and order by?
  end

There is many ways.
And I want to know, which is right.

May be you suggest to read some articles or book?

Thank you.

Re: How to correct use MVC in rails?

It's usually best to keep your view code as simple as possible,  there are three basic things you can do to keep the view code simple:

1) put logic in the controller.
2) user helpers to do complex things related to generating HTML for your view.
3) CAREFULLY enhance your model when it makes sense,   don't put stuff in the model unless it is directly related to the underlying data the model is encapsulating.

You want your view code to look somewhat like the HTML it will ultimately render.  There is nothing technically wrong with ugly view code,  your app will still work.  Clean view code means you are using all the power of rails in the proper way, IMHO.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to correct use MVC in rails?

BradHodges wrote:

It's usually best to keep your view code as simple as possible,  there are three basic things you can do to keep the view code simple:

1) put logic in the controller.
2) user helpers to do complex things related to generating HTML for your view.
3) CAREFULLY enhance your model when it makes sense,   don't put stuff in the model unless it is directly related to the underlying data the model is encapsulating.

You want your view code to look somewhat like the HTML it will ultimately render.  There is nothing technically wrong with ugly view code,  your app will still work.  Clean view code means you are using all the power of rails in the proper way, IMHO.

disagree with point one.
I prefer to put logic in the model.
"Fat model and skinny controller".