0

I was wondering if it was correct to do the following in the model:

get_partial
    #logic...
      return "_partial_name1"
    #more logic
      return  "_partial_name2"
    #more logic
      return  "_partial_name3"
    else
      "_partial_name4"
    end
end

and in the view :

<%= render @product.get_partial %>

There is NO erb code in the model, only logic choosing wich partial it should display. I tested it and it works, but I wanted to know if it is correct from an MVC point of view. If not, where should this logic be placed?

Syl
  • 3,719
  • 6
  • 35
  • 59

3 Answers3

3

Your model should never care what view to render, ever.

If you need logic in deciding what to do, use a view helper.

sevenseacat
  • 24,699
  • 6
  • 63
  • 88
1

In the view, I think you can just put the if..else logic there.. You wouldn't want to put this logic in the model either way. Perhaps you meant controller, but I think putting it in the view is the best way

<% if (condition) %>
    <%= render 'partial1' %>
<% else %>
    <%= render 'partial2' %>
<% end %>
yeenow123
  • 816
  • 3
  • 15
  • 27
  • My logic is a little bit more complicated, but following you i should do switch (@product.partial_logic) case 1 case2 case 3 ... ? Isn't it the same thing with more code in the end? – Syl Oct 22 '12 at 14:06
  • Switch or if/else, essentially the same. Partials are used in views only, so they should not have a reference to them in a model or controller as Karpie said above. – yeenow123 Oct 22 '12 at 15:13
0

Ok, so it seems the best course of action would be to use helpers. I will do that.

Syl
  • 3,719
  • 6
  • 35
  • 59