Different views for subclasses

2019-05-29 05:34发布

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条回答
冷血范
2楼-- · 2019-05-29 06:16

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.

查看更多
登录 后发表回答