In a model, I want to get a file's contents without rendering it. Say the file is a .erb file. I want to store its contents in a database, and then later on, I'll evaluate the string so that it replaces the variables in the .erb file with actual values.
Is there a method like render_to_string that doesn't actually evaluate the .erb part?
I'll ignore any reason you would want to do something like this. Ok so, you're looking to read files, this is done with plain Ruby:
File.read 'path/to/file'
That's how you read any file in Ruby. For a view in Rails you'd have to specify the path:
File.read Rails.root.join('app/views/some_view_dir/your_view_file.erb')
Just replace some_view_dir/your_view_file.erb with your actual view.
Reading files this way gets just the raw content, and you can do this with any file type.
Related
I am trying to figure out how to take a string that is the file contents of a mp4 file and write a properly formatted mp4 file. Currently I am just throwing the string into a file and slapping a .mp4 extension on it, but this resulting file cannot be played by any video players (I am assuming because of all the missing meta data).
def write_mp4(mp4_string)
file = File.new('movie.mp4', 'w')
file.puts(mp4_string)
file.close
end
For context, I am doing this in a Ruby on Rails application, not sure if that changes anything. Please help thanks.
Use "wb" mode, which will suppress EOL conversions and set the encoding properly
Use write, not puts, as the latter inserts an extra EOL
You could use a shortcut: File.write('movie.mp4', mp4_string, mode: 'wb') - or even File.binwrite('movie.mp4', mp4_string).
Of course, make sure the string actually contains a correct file before - for example, if mp4_string.encoding doesn't return #<Encoding:ASCII-8BIT>, you probably done goofed somewhere before the writing step, too :)
I have a series of folders that all have files I need linked to the database (via there file path). One option I could do is manually insert all the file paths into my database, however, this can be painful as the number of folders will keep increasing and manual uploading will take too much time.
Is there a way to write a ruby helper function that will search these folders and automatically add the path to the files into a column in my database?
All the file paths have a recognizable pattern, for example:
Tel/a_1/poi1/names.csv
Tel/a_2/poi1/names.csv
Tel/a_3/poi1/names.csv
I need a function that will occupy a field in my database with the path of each of these names.csv files. Very new to ruby and rails, so any help is greatly appreciated. Also, please let me know if anything is unclear.
Something like this should give u all the filenames in the folder, for you to manipulate:
Dir["Tel/**/**/*.csv].each do |file|
* update attribute of your model with the path of the file
end
Read about the Dir object too.
Thats a example to get all files.
Dir["Tel/a_*/poi1/names.csv"] return a Array with path about all files.
I am trying to upload a file in rails (using paperclip), and I want to process some of the file data before letting paperclip send it off to s3 storage. In my controller, I just grab the file parameter (which does give me a file) and then I try to read the lines into an array
csv_file = params[:activity][:data]
array = IO.readlines(csv_file.path)
The problem is, I'm only getting the last line of the file. I tried using .rewind, but still get just the last line.
I dislike readlines and I always use regular expressions. Try this.
End of line - \n
Handy block structure to ensure that the file handle is closed:
File.open(csv_file.path) do |f|
a = f.readlines
process a...
end
Reading a whole file into memory might not be a good idea depending on the size of the files.
I want to load a xml file into a collection, and I will need to access this on each and every page request.
I only need to load it once, where/what point should I do this?
You can create an initializer to load the xml file and put into a constant.
config/initializers/load_xml_collection.rb
The most straight-forward way would be to set it up as an initializer. Create a new file in #{Rails.root}/config/initializers called load_xml_file.rb (or something a little more descriptive)
Then, within that, you could do something along the lines of:
SETTINGS_FROM_XML_FILE = method_to_read_xml
This will be executed once when your app is loaded. You'll also be able to access SETTINGS_FROM_XML_FILE anywhere in your application.
The only caveat is that if the file changes, you'll need to re-start the application, or come up with a more sophisticated way of loading the details you need.
My current project requires me to assemble a .zip file containing HTML and text-only templates for a user to download, for importing into an email marketing program.
I've inherited this project, and currently the code uses a "fake" model (that is a model that does not directly correlate to a database table), in which it stores the entire template in a string, using dynamic variables to populate certain areas. The "fake" model then has a method for creating a zip file.
It seems to me that there has to be a better way to do this. I was wondering if there was a way to move the template into a .erb/haml file, and then write a method that would populate the file in preparation for being zipped up? Basically, is there a way to render an HTML and text file, without actually having to display them?
Thanks for any help.
Just write the action and view to render the html and text as normal then use the render_to_string method to assign the content to strings
http://apidock.com/rails/ActionController/Base/render_to_string
Then you can run the Model method (hopefully a Class method) to create the zip file using the content you now have in instance variables
Please have a look at the cells plugin.