Where to load data for things like a sidebar in MVC frameworks?

This seems extremely basic, but I’ve read a lot of related questions and haven’t found a proper answer.

Using Ruby on Rails or other similar MVC framework, how am I supposed to load data that is displayed in sidebars on multiple pages?

Options I know of, with the problems I see:

  • Load it in every controller method that needs it: causes duplication of code;
  • Load it in a method on the ApplicationController, which is executed by all controllers: would cause data to be loaded even when not needed;
  • Load it directly from the model in the view: not MVC?

I’ve asked this elsewhere and some people told me to “look into [insert another pattern here]” or “use Ajax calls to get the data”. Although those might also work, I’m looking for an answer regarding MVC frameworks on the server.

Typically, I think the duplication is best handled by pushing the shared code up into a superclass (either in ApplicationController or some other superclass your controller inherits from) as a private method. I think nowadays DHH wants you to put your shared code in modules (erm, i mean concerns), and include those in your controllers, as opposed to shoving everything in ApplicationController.

Depending on your situation you’d either run the code in a before_filter on each controller action where you have your sidebar, or put the before_filter up in a superclass and call skip_before_filter when you dont want the code run before a specific controller action.

Also, its certainly possible to use ajax to accomplish this but unless you have a really good reason, I would avoid doing it that way.

There are also times when I think calling class methods on a model directly from a view is perfectly OK. Like getting simple counts, for example.

2

You may be having trouble if you are thinking of all MVC as a 1 to 1 relationship of View to Controller and a View per Page. A view is not necessarily a page and a page is not necessarily a single view. Commonly they are 1 to 1, but often there is more to it. You might have pages made up of a handful of views, though there will usually be one master view that correlates to the current controller.

Rather than make it part of multiple controllers, you can dedicate a new controller to the sidebar and call it SideBarController. It has its own view.

In ASP.NET MVC land, I’d call it a partial view. Basically a fragment of markup that is included in a master page or set of pages. A master page is often a template page with header, footer, left nav, and main content. In RoR land, I think its done using explicit render

Example, my ProductsController returns a view, the Products index page renders, and inside that template page there is a markup tag that calls SideBar.

Not touched Rails in years, but I think:

render template: "sidebar/show"

Or simply:

render "sidebar/show"

Is enough since it Rails knows its a different controller with the embedded slash.

http://guides.rubyonrails.org/layouts_and_rendering.html

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật