I have an XML file containing data and I can easily parse it to insert the data into my rails database. The only problem is - in which directory should the file go into ("public" seems wrong, since the initial data in the database should not be public), and how do I refer to that file in the seeds.rb file (i.e. what prefix will guarantee that that file will be found).
This is a somewhat silly question but I haven't touched rails for a time now and they keep changing the directory structure...
I'd simply put the file in a folder like /db/data_source because seeds.rb lives in /db so it keeps overall logic.
You can reference any file using:
"#{Rails.root}/path/to/file"
Related
I have been given a task to upload a file and store it in a different location. And I need to return a seed file which should be used to retrieve the file again.
I'm done with all sorts of things but I can't get "what is a seed file and how can generate it and how can I retrieve the file later with that"
can someone help me with this?
Thanks for helping!
Not sure of your context, but seed is usually a file (or a template) that contains some default values to begin with.
I am using active storage with Rails 5.2. I am following the EdgeRails guide, and have configured Active-Storage to use the local disk.
The file uploads work great when I am using the Rails App.
However, the problem is that I need to physically access those uploaded files without using Rails as a mediator.
A query for where the files are stored returns this:
url_for(#employee_staff.avatar)
=> "/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBGUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e76664d247cb5437fe1cd11f7ee0ded24f95aee2/profilepic3.jpeg"
I am trying to figure out where this file path is saved in my local disk. So far, I've had no luck.
Any explanations about how Active-Storage works and where I can see the uploaded files are greatly appreciated.
On your local development machine (since you mentioned local disk), you should have a file config/storage.yml that has a block similar to below:
local:
service: Disk
root: <%= Rails.root.join('storage') %>
In this example above, I'm storing the files in a folder named storage in the root of my rails application. Inside that folder you'll find nested folders that aren't meant to be navigated via file explorer/Finder (but you can).
Thru your rails app, via views for example, you'd use the url helpers which aren't well documented yet.
From a rails console, you can try, for a model named Foo, with a has_one_attached :photo
Foo.last.photo.blob.key
It should give you a ~24 character string.
The first 2 characters are a subfolder inside the folder I pointed you to above
The next 2 characters are a subfolder inside that
Inside the subfolder is a file with the name that matches the key you printed out above (no extension). That's your file.
If you have variants:
variant = #employee_staff
.avatar
.attachment
.variant(resize: '100x100')
.processed # If variant is not processed
variant.service.send(:path_for, variant.key) # Absolute path to variant file
ActiveStorage::Blob.service.path_for(#employee_staff.avatar.key)
As seen in this Answer
How can I load data from yml files onto a HUGO template? I am having trouble understanding the documentation, what would be the steps?
I am using the hyde template.
Given a Yaml-file named mydata.yml:
Put this file into the folder /data inside your Hugo project. If it doesn't exist, create it. The exact name is important.
From the template you can then access the Yaml file as a data structure with $.Site.Data.mydata. Nested elements can be accessed by so called dot chaining: $.Site.Data.mydata.author.name
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 using tolk for translation, but tolk takes all my values from en.yml and dumpes them in es.yml overwriting the existing content .
There are some stuff i don't want to be overwritten , so when i am searching for the es translations, i want rails to look in both es.yml and es.defaults.yml
( and so, i can keep isolated what i generate with tolk, and what remais the same )
Is there a way i can do this?
Thanks
Rails loads every file in the config/locales/ directory, so it will probably already work like you're suggesting. You can even organize it further than that, according to the I18n Guide:
http://guides.rubyonrails.org/i18n.html#organization-of-locale-files
However, I think that with duplicate key structures, Rails will probably override the values of the earlier loaded (sorted by file name) locale file with the values of the later loaded file. So please try to avoid duplicate keys.