I am new to Ruby and Sinatra but I've read a bit about Rails. One of the things Rails masters tell us to do is to leave code out of the templates.
Well, here is my code, right where the information is needed in my template. It works but, for the sake of learning the best practices, how could I move this out of my template into my app.rb file without repeating it in every block of code (CRUD) which relies on the template?
<h2>Status</h2>
<ul>
<li>Received<span><%= Inbox.count %></span></li>
<li>Sent<span><%= Outbox.all(:processed => 1).count %></span></li>
<li><Scheduled<span><%= Outbox.all(:error => -1).count %></span></li>
<li>Error<span><%= Outbox.all(:error.not => [-1,0]).count %></span></li>
</ul>
Thanks for the pointers.
Just load the counts into instance variables where you define the routes:
get('/or_so') do
@inbox_count = Inbox.count
@sent_count = Outbox.all(:processed => 1).count
@scheduled_count = Outbox.all(:error => -1).count
@errored_count = Outbox.all(:error.not => [-1,0])
erb :your_template
end
If you're going to load these over multiple pages, there's also before
before(/this|insane|regexp|to|grep|locations/) do
# assign variables
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With