Different views for subclasses

拟墨画扇 提交于 2020-01-02 20:18:11

问题


I'm using STI subclasses and want to direct to different views for the different subclasses. At the moment I'm routing the subclass topic to the main class article like this:

 resources :topics, :controller => 'articles'

Is there an easy way to direct to different views?


Edit

The best way I've found of doing this is:

  <% case%>
    <% when @article.type == 'Topic' %>
      <%= render 'topic' %>
    <% else %>
      <%= render 'article' %>
  <% end %>

回答1:


If you name your views smartly enough, you could just do

render @article.type

or

render :partial => @article.type

Otherwise you could define a method in all your STI subclasses that returns the name of the partial/page to be display, that way you'll avoid all the if/else logic in your controller.

Another approach would be to have separate controllers for the different subclasses, but that is not a good design. If you were to take such a path, than maybe STI wasn't the best approach after all.



来源:https://stackoverflow.com/questions/8692980/different-views-for-subclasses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!