File path in rake task on heroku - ruby-on-rails-3

I have a rake task in Rails which job is basically to just open a file and write some text, I have
def self.log
File.open("#{Rails.root.join("app/views/main/log.html.erb")}", "a+"){|f| f << "sometext" }
end
My file is at /app/views/main/log.html.erb
This works perfectly fine locally, but once I upload onto Heroku it doesn't write anything to the file (no error message either). Suspecting the file path is incorrect, or anything else I don't know about Heroku? Thanks!

Heroku's filesystem is read only. It is highlighted in this article: https://devcenter.heroku.com/articles/read-only-filesystem. Hope that helps.

Related

Laravel 403 error when displaying images from storage folder

I am unable to access files saved to the storage folder. I'm able to upload, files, save files. If I run for example the size method it gets the file size of the uploaded image but when it comes to displaying the file, I get a 403 error. I used the laravel artisan command to create the symlink, I've tried manually creating the symlink. I've checked to verify that follow symlinks is in my apache config, I can cd into it from shell the permissions are 777 (I had it 755 but in trying to figure it what is wrong I changed it to 777) ownership of the symlink and files inside are all the same user and group as every other file in the public directory.
I'm super tired so maybe I'm just missing something obvious, but I can't for the life of me figure out what is wrong. The file clearly exists, its visibility set to "public". Is there any reason why I'd be able to write the directory but not display images saved there?
Edit 1:
web
app
bootstrap
config
database
error
node_modules
public
resources
routes
stats
storage
temp
vendor
Is the basic structure, with a symlink inside public pointing at storage/app/public
the filesystems for my storage folder config is:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
I haven't really edited anything from the basic laravel install at this point. I did read someone else having a similar problem, said their issue is they weren't allow to access direcotires outside of their document root. So they just had all uploads go to their public folder instead of using the storage folder. I do have my document root set to my public folder. Could that be a problem? (I can edit my apache file if needed)
Ok - got some sleep and this morning looked over everything and realized that when logged in as the site owner, everything looks fine, however when logged in as root it shows the link as broken. Basically artisan creates an absolute link, which /storage/app/public is fine as the site owner because its a jailkitted account whose "root" directory is the web folder. However it was actually creating a symlink to the system root, of which the normally account doesn't have access to so it was returning a 403
Basically I just made the as a relative link instead of an absolute one by removing the broken symlink laravel created and while in the public directory entering:
ln -s ../storage/app/public storage
I had the same issue, after a lot of debugging i managed to identify the actual problem that caused 403 error.
Solution is simple, in
Config/Filesystem.php
where you define your public_path and storage path, you must have same/identical public_path name and storage_path end directory name.
Example:
Incorrect:
public_path('brands') => storage_path('storage/app/public/brandimages');
This will generate 403 error, "since public_path('brands')" is not same as "storage_path('../../../brandsimage')".
Correct:
public_path('brands') => storage_path('storage/app/public/brands');
Now the public_path('brands') and "storage_path('../../../brands')" are same, therefore, correct symlinks will generated,thus solving 403 error.
Generate symlinks with following artisan command
php artisan storage:link
if relative links need to be generated, than use following command
php artisan storage:link --relative
My hosting is a clud server, and my site path is /httpdocs
The solution worked for me, was:
from folder /httpdocs/public, execute ln -s ../storage/app/public storage
then everything works fine.

Rails on AWS - getting Permission denied for mkdir /uploads

I know this question has been asked a lot, but no one seems to have my exact problem.
I ran into the permission error on my rails app, running on an AWS EC2, so I promptly ran chmod 666 -R public/uploads
However, once I did that, I got another permission denied error, this time for mkdir on uploads/images. So I ran chmod on it too. But this time, no difference.
I am using Carrierwave to upload the images. I'm a bit of a newb with the linux commands. Any help would be greatly appreciated!
Turned out to be an issue with Carrierwave. The gem wasn't writing files when 'version_name' was used in the url. So I removed it and the files saved fine.
However, image_url, the documented method of getting the url, returned the url with the version_name, rather than the actual url to file. Calling the object attribute declared for the uploader returned the right one.

rails console list directory

I am trying to load a script into Rails console. In order to load the file, I need to do something like:
load scripts/my_script.rb
# This doesn't work... gives this error:
NameError: undefined local variable or method `scripts' for main:Object
load ./scripts/my_script.rb
ArgumentError: wrong number of arguments (0 for 1)
load "./scripts/my_script.rb"
LoadError: cannot load such file -- ./scripts/my_script.rb
# I feel like supermode. Tell me whyyy?
I suspect that I start off in my base rails directory, so if Rails console's "ls" command did as the terminal did (it doesn't), it would list:
app/
config/
scripts/
Gemfile
etc
What commands can I use from within Rails console to navigate around, list the contents of the current directory, and reveal what directory Rails console is currently in?
Or if this is not possible, please spare my sanity and explain why the load command with the directory and filename (and permutations) are not working.
Extra note: I need to reload different files as I make changes to them - so starting the console passing a single filename is no good.
Do you want to do some operations on some ruby files from console?
You can use
Dir.pwd
to get current Directory, and can use other File related methods to require a file.

How can I run oink in heroku?

I'm having a problem running the oink gem on my app in Heroku. I've included it in my gemfile and gemfile.lock, uploaded those, and it installs. It even creates the oink.log (which I have no way of viewing, unfortunately). When I run
heroku run bundle exec oink --threshold=0 log/* --app my_app
I get
Running bundle exec oink --threshold=0
log/delayed_job.log log/development.log log/oink.log log/production.log log/test.log attached to terminal... up, run.3
/app/.bundle/gems/ruby/1.8/gems/oink-0.9.3/bin/../lib/oink/cli.rb:88:in get_file_listing':
Could not find "log/delayed_job.log" (RuntimeError)
from /app/.bundle/gems/ruby/1.8/gems/oink-0.9.3/bin/../lib/oink/cli.rb:86:ineach'
from /app/.bundle/gems/ruby/1.8/gems/oink-0.9.3/bin/../lib/oink/cli.rb:86:in get_file_listing'
from /app/.bundle/gems/ruby/1.8/gems/oink-0.9.3/bin/../lib/oink/cli.rb:59:inprocess'
from /app/.bundle/gems/ruby/1.8/gems/oink-0.9.3/bin/oink:4
from /app/.bundle/gems/ruby/1.8/bin/oink:19:in 'load'
from /app/.bundle/gems/ruby/1.8/bin/oink:19
I've tried running each of the individual files, too, and get the same result. This command runs fine on my local machine.
In my production.rb file, I have
config.logger = Hodel3000CompliantLogger.new(config.paths.log.first)
config.middleware.use( Oink::Middleware )
as configuration.
Can you enlighten me on what I'm doing wrong here? My understanding is that the logs are read only, but I don't know if that means they're only accessible through the heroku logs command. If there's a way I can see the oink.log file, too--knowing how to do that is also appreciated, or knowing how to see it in the actual Heroku log using heroku logs.
UPDATE: The configuration for oink shown above allows the commands to be run successfully on my localhost.
Thanks!
-Andrew

Heroku rake db:seed permission denied when opening .xls with spreadsheet gem

I have been using the spreadsheet gem from http://spreadsheet.ch/ to read in seed data to a Rails 3 application.
I was using
Spreadsheet.open "xxxx"
which ran fine on my dev machine but when pushed to Heroku errored with "Permission denied"
The guide http://spreadsheet.rubyforge.org/files/GUIDE_txt.html is only very basic help.
Down we go into the 'long series of debugging questions' rabbit hole:
"Is the spreadsheet checked into git, such that it would show up on Heroku?"
"Is ruby spreadsheet trying to write to a file that's in one of the read only parts of heroku?"
"Are you using Spreadsheet.open or ?"
I guess in general it would be nice if you could include a stack trace of the error Heroku is giving you.
Perhaps: heroku logs within your project directory might be of help, if the errors are recorded there?
Chalk this one up to duh
Spreadsheet by default opens in read/write which Heroku does not allow. A little ri research revealed that Spreadsheet.open can be passed the 'r' flag for read-only. Here is the ri output
>>ri Spreadsheet.open
Spreadsheet.open
(from gem spreadsheet-0.6.5.4)
------------------------------------------------------------------------------
open(io_or_path, mode="rb+", &block)
------------------------------------------------------------------------------
Parses a Spreadsheet Document and returns a Workbook object. At present, only Excel-Documents can be read.