Deleting folder containing file - ruby-on-rails

I want to send the file file.txt from my Rails controller using send_file, and then delete the folder containing it.
send_file("#{Rails.root}/public/folder/file.txt")
system("rm -rf #{Rails.root}/public/folder")
When I tried this, file.txt was correctly sent, file.txt was correctly deleted, but somehow folder was not deleted.
How can I make it delete folder?

Try to delete the folder directly with the native ruby method instead of a system command:
require 'fileutils'
FileUtils.remove_dir "#{Rails.root}/public/folder", true
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html#method-c-remove_dir

Related

accidentally removed log folder in rails .gitignore, log folder not being generated on pull request

I'm new to git and rails. I checked out a rails project and had a bunch of log files come in with it. I wanted to remove the log files and add an entry into .gitignore to ignore contents of the log directory. I think I screwed up because when my colleague tried to checkout from master, his log directory didn't even get generated. I think instead of ignoring contents of the log file, I ignored the entire log directory. What's the best way to change this .gitignore file to only ignore log files NOT the log directory itself? Also I don't think I need all these things in the .gitignore. What's a nice clean and simple .gitignore for a rails project?
*.sublime-*
.bundle
db/*.sqlite3*
log/*.log
*.log
tmp/**/*
tmp/*
Gemfile.lock
doc/api
doc/app
*.swp
*~
.DS_Store
Thank you in advance.
Git ignores empty folders, in fact it doesn't really know they exist (it knows about trees pointing toward file contents). Since there is no file content to point to, there is no tree and hence git will not create a folder.
Common way around this is to add empty file within the folder. Convention is to name this file .keep. Add such file to the empty folder, add it to the index, commit and push and folder will be created on checkout.

How to delete an app from a Rails project?

Initially I made 2 apps (app_a and app_b) in a single project in Ruby. Now I want to delete one (say app_a). How should I do so? Is deleting the app folder sufficient?
You need to drop your related database and then delete the app directory
# from app directory
rake db:drop
cd .. && rm -rf app_a
Rails generator command creates a copy of the framework. The app directory id self contained. Deleting it is enough. If you are using sqlite as your database then you can skip first command.
Deleting the app folder will get rid of it. Be sure to clean up your routes file if needed as well as get rid of a database table if it was created.

I cannot get git to ignore my tmp folder of my Ruby on Rails project

So I tried everything
but to no avail
I keep getting the following error
"fatal: cannot use tmp/ as an exclude file"
I have even tried using /tmp and tmp/* but none of these two work either .
Finally I deleted the tmp folder in frustration and i found that git now works perfectly
SO I have two questions.
The obvious one being . How do I get this to work?
Is it possible for me to run my ruby on rails applications without the tmp folder?
The obvious one being . How do I get this to work?
Add /tmp to your .gitignore at the root of your Rails app. Make sure you add and commit this .gitignore before committing /tmp — you'd have to git rm it to make it disappear from the repository again.
Is it possible for me to run my ruby on rails applications without the tmp folder?
No. Why would you? Rails needs to be able to write files there in order to work properly. You can only symlink it somewhere else, but that won't really solve the issue if the files are included in your repository. See also: Rails3: Change location of temp (tmp) directory
It may have to do with the way git works with cached files that the folder may still show up after including it in the .gitignore file.
Try git rm . -r --cached in the command line to clear out the cached files.

Ruby on Rails Tutorial - 5.26 - Sublime Text "Unable to Save" new file "spec/support/utilities.rb"

I am using Sublime Text 2 while following Michael Hartl's Ruby on Rails Tutorial.
The specific portion of the tutorial to which I am referring can be found at http://ruby.railstutorial.org/book/ruby-on-rails-tutorial (ctrl+F "Listing 5.26").
I am able to create the spec/support file. However, when trying to create the spec/support/utilities.rb file, I receive the message "Unable to save ~/rails_projects/sample_app/spec/support/utilities.rb".
Does anyone know why this might be?
Someone on the Sublime Text forum seems to have had the exact same problem: http://www.sublimetext.com/forum/viewtopic.php?f=3&t=8570&p=36922#p36922
This issue sounds like it's a result of incorrect permissions or ownership of the folder. Change directories (cd) so you're outside of the folder where you're creating the .rb file and type:
ls -l
This terminal command lists the permissions attached to all files/folders in that directory. If "root" is listed as the folder owner, change its owner by typing:
sudo chown YOUR_COMP_USER_NAME FOLDER_NAME/
You should now be able to save files from inside that folder.
To diagnose this, first find out if it's an issue in Sublime or your file system:
Does that file already exist? Try looking for it on your file system (not using Sublime).
Verify that you have permission to write to that file. Use "ls -la" on the command line to show the file permissions.
Are you able to create and/or save that file using any different editor, for example TextMate, or Notepad?
The following sublime plugin fixed the Unable to save... bug
https://gist.github.com/3779601
The folder spec/support doesn't exist, and sublime won't create the missing folder, so it errors.
You just need to make the spec/support folder , then sublime will save the file.
I also highly recommend installing the AdvancedNewFile plugin ( Video of it action thanks to Jeffrey Way and NetTuts+ ) , which you can grab straight from Package Control.
It creates files, parent folders if needed, and if you try to create a file that already exists, it opens it instead.
This can happen if you are trying to create the file within a directory that does not currently exist. For ex. I was unable to save
~/rails_projects/sample_app/app/views/shared/_error_messages.html.erb
via the "subl" command from the Terminal because I was missing /shared/ folder. Hope that helps.
so #knice almost had it, I ran into the same problem with permission when starting my first rails project on mavericks.
as mentioned if you run ls -l you'll see your folder / files listed with their permissions
I solved this by changing ownership recursively with the following command from outside my project directory
sudo chown -Rv <your_username> <your_path_and_foldername>
for example if you're in your folder in terminal you should cd .. and then
sudo chown -Rv username ruby_proj/
the -R is for recursive meaning it will apply to all files and folder contained within the folder you specified, and the v after just produces verbose output, showing you which folder and file permissions have been changed.
Hope that helps someone else.

Where is the .rspec configuration file located exactly?

Working through the Ruby on Rails Tutorial and stuck here "To run RSpec and Spork with Autotest, we need to configure RSpec to use the --drb option by default, which we can arrange by adding it to the .rspec configuration file in the Rails root directory (Listing 3.14)."
When I search my file structure I can not find a single file with .rspec file extension? How do I access this file to update the configuration?
If you are using a mac you will not see these files just like you will not see the gitignore file
what you need to do in order to access this file is from the root directory of the app just click (command line)
mate .rspec
this will open the file if it's there, and if not, it will create it.
If you add gem 'rspec-rails' to your Gemfile, you can use the rails g rspec:install command.
This will create the .rspec file in the root of the Rails application. It will also create a spec folder and a file inside it 'spec_helper.rb'
If you do not want to write this command, you may simply create the file manually. If you are using a linux machine, the hidden files can be seen with a <ctrl-h> command. You may also try the ls -a in any unix terminal to see all hidden and non hidden files.
Files on mac that begin with . are not shown by default. In Terminal, if you type ls -a you'll see all of the "dotfiles" that are in the folder.
If this file doesn't exist yet (it probably doesn't) then you just need to create it using your favorite editor.

Resources