Setting up rails dev env - ruby-on-rails

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

Related

What are the correct steps to upgrade Rails 2.3.11 to 2.3.15?

Given the recent security patch for Rails, what are the correct steps that should be taken to successfully update Rails 2.3.11 to 2.3.15?
Run your tests and make sure everything works
Open up a new branch through git (git checkout -b new_rails)
Change Rails 2.3.11 to 2.3.15 in your Gemfile
Change RAILS_GEM_VERSION to '2.3.15' in environment.rb
Run bundle update rails
Run the tests and see if everything still works
If not, reverse back to the old branch by doing git checkout master; optionally delete the new branch by doing git branch -D new_rails
Doing it only on the new branch ensures nothing breaks.

Convert a Rails Gem / Engine to an application

The rails engines feature is pretty good, and I have watched the Railcasts and read the Rails documentation on it. I can see how you can access or override all the relevant components in the engine.
However, say I wanted to drastically modify the engine's code, is it possible to convert the engine back into a normal Rails app, and then take it from there. Is there anything else involved other than copying the directories in the gem over an empty application directory?
I am looking at this engine:
https://github.com/ging/social_stream
Yeah you can, just go to https://github.com/rails/rails & hit the fork button to fork the repository to your github account (assuming you already have one setup). Afterwards, clone the forked project to your local machine with:
git clone your_forked_repository_url.git
If you don't feel the need to fork your own version run:
git clone git://github.com/rails/rails.git
At this point you can make modifications to your heart's content. To use a local copy of the gem in a rails application add the following to your Gemfile (replacing the old rails gem):
gem "rails", :path => "/somewhere/your_rails_project"
All of this and more is highlighted in http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html

How to run "rails s"

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.

Rails vendor plugin failure in production environment

I expect this to be a n00b problem.
I'm attempting to use heroku with my rails app. Everything works fine in development (local machine), but when I push to heroku I get the following:
==> dyno-1931938.log (crash) <==
/home/slugs/258915_4fd8878_0dbe-1413ed77-735c-469d-924e-619b28cdcbac/mnt/.bundle/gems/ruby/1.8/gems/activerecord-3.0.0/lib/active_record/base.rb:1016:in `method_missing': undefined local variable or method `acts_as_paranoid' for #<Class:0x2b469869b658> (NameError)
from /disk1/home/slugs/258915_4fd8878_0dbe-1413ed77-735c-469d-924e-619b28cdcbac/mnt/app/models/my_model.rb:17
lines 16 and 17 of my_model.rb are:
class Contact < ActiveRecord::Base
acts_as_paranoid
'acts_as_paranoid' is a vendor plugin that was installed locally via:
$git clone https://github.com/goncalossilva/rails3_acts_as_paranoid.git
What am I doing wrong that heroku is ignoring the plugin?
UPDATE:
I cloned the repo from heroku, and the directory for the plugin exists, but is empty. My other plugins (i.e. ssl_requirement) have the expect lib/ and init.rb. Obviously the code needs to be there to work. What now?
Cloning the source retrieves the code from github, but it doesn't install the gem into your app. Normally you'd install the gem on your local machine like this:
gem install acts_as_paranoid
or
sudo gem install acts_as_paranoid
Then tell Heroku about the gem by adding this to your .gems file:
acts_as_paranoid
or better yet, specify the version that you expect, too (otherwise Heroku will grab the latest every time you push up new code to your app, which could cause unexpected breakage):
acts_as_paranoid --version x.y.z
And make sure your config/environment.rb has an entry for the gem so its absence will be detected at launch instead of later:
config.gem 'acts_as_paranoid', :version => 'x.y.z'
PS: All of the above will be somewhat different if you're using Rails 3 and/or Bundler to manage your gems.
It turns out vendor/plugins/my_plugin_directory had fallen out of the git tree. I resolved the problem by:
$cd rails_root_directory
$git fsck
$git rm --cached vendor/plugins/my_plugin_directory
$git add vendor/plugins/my_plugin_directory/*
$git commit -am "my_plugin added to repository"
$git push heroku master
and all is right now.

Updating an unpacked and customised gem

We have an interesting scenario I need to sort out:
1) We have an existing application running with an unpacked gem
2) The application has some customisations to the unpacked gem
3) I would like to somehow "merge" a new version of said gem into this unpacked gem to bring it up to date.
Any ideas on a nice way of doing this?
All the code is in git, although the gem source is in a different repo.
You can compute the diff with git.
Create a new empty repository and copy the customized Gem.
Add the changes and commit.
$ mkdir thegem
$ cd thegem
$ git init
$ cp -r /path/to/gem/* .
$ git add *
$ git commit -m "Custom Gem"
Now download a snapshot of the original Gem and replace all the content of the folder (exept the .git folder). Add and commit.
Now you can view the differences between the first and second commit to know what changed in your customized Gem.

Resources