Great thing about Volt - reuse between FE and backend. File manipulations and other ops asynch execute and return to frontend simply with promise
First off, lets make a new volt app called server_stats
$ volt new server_stats
Now, if we hop into the code we can get to business. Let's say we wanted to display some simple information about Ruby on our machine from the backend server environment. Obviously this would be impossible to do clientside, but it is a perfect candidate for a task in Volt. To create one, we simple use a CLI helper:
$ bundle exec volt g task Stats
This will create a file in our tasks
directory with a Volt::Task
model that will be able to do the work we need.
To create the task itself, we open up our StatTask
model and just begin to add some logic.
class StatsTask < Volt::Task
def show_stats
{ version: RUBY_VERSION,
description: RUBY_DESCRIPTION,
platform: RUBY_PLATFORM }
end
end
And now in our `main_controller we can run this task for our index view:
...
def index
StartTask.show_stats.then do |stats|
page._info = stats
end.fail do |err|
page._info = error
end
end
...
and now in main/index.html
we can access this in the view code inside <:Body>
<:Body>
<h3> Ruby Version </h3>
<p> {{ page._info._version }} </p>
<h3> Ruby Description </h3>
<p> {{ page._info._description }} </p>
<h3> Ruby Platform </h3>
<p> {{ page._info._platform }} </p>
Fire up our server with
$ bundle exec volt s
And if we visit localhost:3000
we can see all these stats live.