开发者

partial in two different model's template

I made this left navigation bar, like the picture below I want in in both Drummer model and Video model,

partial in two different model's template

So I choose to use partial _drummers_list.html.erb

<div id="columnleft"&开发者_开发知识库gt;
<img src="/img/alldrummerup.png" alt="" width="150" height="60" />
<% @drummer_list.each do |d| %>
<%= link_to d do%>
<%= d.first_name + ' ' + d.last_name %>
<% end %>
<% end %>
<img src="/img/drummerdown.png" alt="" width="96" height="42" />
</div><!--columnleft-->

The problem is the @drummer_list instance variable only available in the drummer controller, so I have to write it again in the Video controller, because of the DRY principle, I think it make sense use some method only write @drummer_list once and use it everywhere

I didn't spent too much time to learn the Ruby language, it's the foundation of Rails, I think that's why I can't figure it out.


You can define a method in application_controller.rb that would be shared for both of the other controllers. Maybe like this:

def find_drummers
  @drummer_list = Drummer.find :all
end

Then just call it from the drummer controller and video controller. I'm assuming your actual method might be more complicated. You could also put a finder method on your Drummer model object and call that from the two controllers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜