Widget data across multiple controllers

Let's say that I have a widget that displays summary information about the number of posts or comments that I have on the site.

What is the cleanest way to store this information through controllers?

Including instance variables in an application controller seems like a bad idea. The presence of a filter before downloading data for each controller smells like duplication of code.

Should I use a plugin like Cells plugin ( http://cells.rubyforge.org/ ), or is there an easier way to do this?

+3
source share
3 answers

, , . , , ApplicationHelper, (), . , .

+1

- :

/application.rb

def load_sidebar
  @posts = Post.find(:all)
end

, :

before_filter :load_sidebar, :only => [ :index ] #load from application.rb file

.

0
def load_sidebar
  @posts = Post.find(:all)
end

You mentioned that you showed summary information. If you really do not want to load all your messages into memory, you can do the following.

def load_sidebar
  @post_count = Post.count(:id)
end
0
source

Source: https://habr.com/ru/post/1697423/


All Articles