When I'm starting a rails app I usually go through the same process of getting my dev env set up. Adding pry-byebug or various guard-* gems, and initializing a Guardfile.
Is there a better way to automate this process?
Some ideas I thought of:
Creating rake tasks that will add entries to Gemfile, run bundle install, and bundle exec guard init livereload, etc...
Create a separate command line app that generates the right files and runs the right shell commands.
Is there another simpler way?
You can use the templates method described here. You create a file called template.rb that contains your desired gems, and commands. Then you create a new rails app with
rails new blog -m ~/template.rb
A sample template.rb is
generate(:scaffold, "person name:string")
route "root to: 'people#index'"
rails_command("db:migrate")
after_bundle do
git :init
git add: "."
git commit: %Q{ -m 'Initial commit' }
end
Another way which I find much easier is just to maintain a github repository which contains everything you want in a bare bones Rails app. Then just git pull that into a folder when you want to create a new app. The only thing you'd have to overwrite would be the app's name in application.rb
Here is github repo that is a combination of both of sort, it's geared towards programming on Mac but easily changed.
See Iceman's answer for more info.
Using rails application templates is a great option.
For example if you want to get up and running with guard-livereload and pry-byebug and quickly scaffold a Post resource, then the following application template (it's just a ruby file) saved in ~/sandboxy.rb would work:
gem_group :development do
gem 'pry-byebug'
gem 'guard-livereload'
end
run 'bundle exec guard init livereload'
after_bundle do
git :init
git add: "."
git commit: %Q{ -m 'Initial commit' }
end
generate(:scaffold, "post title body:text")
route "root to: 'posts#index'"
rails_command("db:migrate")
git add: '.'
git commit: %Q{ -m 'Scaffold a post' }
Then you can generate a new app with the following command:
rails new some_app -m ~/sandbox.rb
I have a custom gem built as a .gem file that I am trying to reference from my Gemfile. I have placed the .gem file, along with the .gemspec, in the vendor/gems folder. My Gemfile has the following line:
gem 'umlgrader', '1.0.0', :path=>'vendor/gems'
When I run bundle install, it claims to have found the gem but it says it is "using" the gem, rather than "installing" it, even though the gem was not previously installed on my machine. When I try to run my app, I then get a NoMethodError when it tries to call any of the methods in the gem. Why isn't Bundler installing the gem?
I have gotten it to work by unpacking the gem in that directory and then editing the Gemfile as follows:
gem 'umlgrader', '1.0.0', :path=>'vendor/gems/umlgrader-1.0.0'
This solution is less than desirable. I would prefer to be able to install the gem using Bundler since I am trying to deploy the app to Heroku. I have already tried a lot of the solutions I have found online, but I am open to any suggestions.
EDIT:
Some of the other pages I have already gone through and tried:
Bundler: installing a specific .gem file
How to use Bundler with offline .gem file?
How do I specify local .gem files in my Gemfile?
I also noticed a lot of people suggest pointing to a Git repository containing the gem. I would rather not do this if I don't have to.
The Bundler documentation is somewhat cryptic on that topic, but that is the intended behaviour of Bundler. In order to use the path option you must unpack the gem at the desired location.
But that should be no problem for Heroku. After all, you are still using Bundler, even if the gem is already unpacked. It's just a step less in the gem installing process...
Without Docker
You need to clone the repository from github (or other source) to your custom folder. In example below the steps to reproduce how I use custom path to edit gems in a separeted folder:
In this case I use a custom_gems folder inside /bundle: mkdir /bundle/custom_gems.
cd /bundle/custom_gems.
git clone <gem-repository-source>. Is necessary to clone because if you copy from other folder in your computer probably some files are missed.
Set in your Gemfile: gem '<gem-name>', path: '/bundle/custom_gems/<gem-name>'.
Restart you application.
In docker-compose (or Docker)
With docker is little different, in this case I use a /gems folder inside my rails application folder: mkdir <my-app>/gems.
cd <my-app>/gems.
git clone <gem-repository-source>. Is necessary to clone because if you copy from other folder in your computer probably some files are missed.
Set in your Gemfile: gem '<gem-name>', path: '/bundle/custom_gems/<gem-name>'.
If you use docker-compose, you need to bind folders with volumes config, like below:
volumes:
- ./app/gems:/bundle/custom_gems
With this, your local folder (your machine) copy files inside ./app/gems to /bundle/custom_gems in Docker container.
Restart service.
If you NOT use docker-compose, you need add in Dockerfile some like:
ADD ./app/gems /bundle/custom_gems
I have a gem that is hosted on github and not yet pushed to rubygems, and I added a tag to the master branch of the gem like this:
git tag -a v0.1.0 -m "gem version 0.1.0"
git push origin -tags
and then in a rails application I have on github I edited my gemfile like so:
gem 'your-gem', git: 'git://github.com/your-repo/your-gem.git', tag: 'v0.1.0'
My question is, when I merge in additional changes into the master branch of my gem, my rails application will still point to the last commit before I made the tag? I just want to make sure adding additional changes to the gems master branch will not break anything in the rails app. Thank You
The correct command is git push origin --tags, or git push origin v0.1.0 if you want to push just the one tag, but otherwise yes, your expectation is correct.
See here for more on bundling gems from git repositories:
http://gembundler.com/v1.3/git.html
I have a problem running rails s in Ubuntu. When I type rails s it doesn't start the server, but instead it outputs:
kyala#ubuntu:~/depot$ rails s
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /home/kyala/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
-d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db)enter code here
# Default: sqlite3
-b, [--builder=BUILDER] # Path to an application builder (can be a filesystem path or URL)
-m, [--template=TEMPLATE] # Path to an application template (can be a filesystem path or URL)
[--dev] # Setup the application with Gemfile pointing to your Rails
checkout
[--edge] # Setup the application with Gemfile pointing to Rails
repository
[--skip-gemfile] # Don't create a Gemfile
-O, [--skip-active-record] # Skip Active Record files
-T, [--skip-test-unit] # Skip Test::Unit files
-J, [--skip-prototype] # Skip Prototype files
-G, [--skip-git] # Skip Git ignores and keeps
Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend] # Run but do not make any changes
-q, [--quiet] # Supress status output
-s, [--skip] # Skip files that already exist
Rails options:
-v, [--version] # Show Rails version number and quit
-h, [--help] # Show this help message and quit
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
See the README in the newly created application to get going.
Try to regenerate binstubs:
rm bin/*
rake rails:update:bin
It should do the trick.
For newer (5.2+) Rails versions use
rake app:update:bin
When the script folder is missing from the Rails application folder it shows the above error.
I just copied it from another app and it worked for me.
Before running the Rails server, you need to first create a Rails application.
For example, to create a new app call "test_app", run the following:
rails new test_app
Once your application is created, you can cd into the directory and start your server:
cd test_app
rails server
OK guyz just for the closure... this problem occurs only when we delete some(mostly script) folders in the rails app... (may be.. accidentally.).. I had this issue but was in a wrong app folder...
My first hunch would be that you are not in the root of your Rails application.
On our deployment servers, I have to type
./script/rails s
when in the root-folder of my Rails-app. I think that is because bin\rails is not known there.
If that would not work, it seems to me that you are not at all inside a Rails root folder, which would also explain why rails s did not work.
A Rails root project will contain at least the following directories: app, lib, config, script ....
I've seen a similar issue with Rails 2.x apps. They fire up fine with thin, unicorn and such, but to get just the webrick server I've had to run bundle exec script/server (or for the less careful script/server seems to work). I don't know the root issue at play here, but this seems to tide me over as I don't maintain any rails 2.x code (simply running ChiliProject 3.x, etc.).
We had the same problem.
Be sure you run the 'rails' command in the script folder and not the binary 'rails' that is different
script/rails s
It´s the same that if you go to your script folder and run the command:
cd script
./rails s
Check whether the 'script' folder exists in your application structure.
I had the same issue. I had forgotten to run bundle after creating an app.
From the root of your project directory run:
bundle install
Run the following command inside your project folder:
rails s
Your project folder contains node_modules, Gemfile.lock, etc...
While searching for an answer myself, I ended up trying a few things that proved useful to getting rails s to work for me. This resulted in 658 files changed, 102204 insertions, and 149 deletions.
Look at the file that you're in by running ls.
Run git status.
Run git add ..
Run git commit -m "Notate whatever changes you are adding to github repository".
I tried to run git push and git push master but neither work, "go figure." My guess is that you can't push changes that belong to a different file or branch.
HERE's THE SECRET... For some strange reason, I was working in a different file so I had to run a git pull YourOtherFile. This is where everything started making sense.
Now, I ran another git status to understand what was going on within this file. There was modified and untracked content.
Next, cd back into the other file.
Run git status to view all of your modified and untracked files.
Run git add . and git commit -m "Notate your changes to this repository".
Watch the magic happen then run a git push.
Run gem update bundler.
Then I ran gem install rails_12factor.
Run another git status to view your modification.
Run git commit -m "Successfully added gem rails_12Factor".
Run git push.
I had issues with bcrypt being locked at 3.1.11 so I ran gem install 'bcrypt'.
Run gem install rails_12factor yet again. I believe that I had the "f" in "factor" capitalized.
Run gem update.
Run gem install pg.
Run git add ..
Run git commit -m "Updated Gemfile".
Run git push.
Run gem install 'pg' yet again.
I was running into all kind of issues but it was because I was trying to upgrade my gemfile to Rails 5.
Run gem install railties.
Run gem install activesupport.
If your Gemfile was already in another version of Rails (gem 'rails', '4.2.6'), make sure that you keep it there as there was not a significant difference in Rails 5.
Just Run 'bundle update' command and start your application
Try "rails server" instead of the short form. Maybe you have it aliased for some reason.
Let's say that I have a rails plugin called "wipy" on GitHub. It has a master branch and it has production branch. I am using Rails 2.3.10.
I need to install production branch of this plugin. This is what I do currently:
git clone git://github.com/nadala/demo.git
git co -b production origin/production
cp -rv wipy ~/my_project/vendor/
I could do:
ruby script/plugin install git://github.com/nadal/wipy.git
However I do not know how to pass an indicator that I want production branch.
I tried following but it did not work:
ruby script/plugin install git://github.com/nadal/wipy.git --branch production
Would this blog post help?
Recently I needed to install the Rails 2.3 stable version of the exception notification plugin and so I needed to specify a particular branch in the git repository.
script/plugin install git://github.com/rails/exception_notification.git -r 2-3-stable
The -r option allows you to identifiy the specific branch you're after.
In your case:
ruby script/plugin install git://github.com/nadal/wipy.git -r production
ruby script/plugin install git://github.com/nadal/wipy.git
cd vendor/plugins/wipy
git checkout production