html - An empty string with File-IO in VoltRb -
i'm trying file-reading in voltrb , embed contents of files in views. planned read files on server-side (since client-side doesn't support file-reading), , pass strings of files on client, seems i'm getting empty string files on client.
here code demonstrate problem:
file.txt
file contents main_controller.rb
if ruby_engine == 'ruby' file = file.open("app/files_to_read/lib/file.txt", "r") $file_text = file.read file.close puts $file_text # returns "file contents" on server side puts $file_text.class # returns "string" on server side end module main class maincontroller < volt::modelcontroller model :page def index page._text = $file_text puts $file_text # returns empty string in browser console end ... end end index.html
<:title> home <:body> <h1>home</h1> <p>{{ _text }}</p> <!-- ^ empty string --> my directory tree looks this:
app ├── files_to_read │ └── lib │ └── file.txt └── main ├── assets ├── config ├── controllers │ └── main_controller.rb ├── lib ├── tasks └── views └── main ├── about.html ├── index.html └── main.html why getting empty string file , how fix this?
so 1 thing keep in mind controllers run twice, once on client, , once on server. client 1 different instance ones on server, won't have access $file_text global.
what can though create task reads file , returns text of file. check out @rickcarlino's excellent tasks tutorial video: http://datamelon.io/blog/2015/creating-volt-task-objects.html
thanks
Comments
Post a Comment