Gemfile require gem in all environments except one - ruby-on-rails

I have rails application with a lot of environments: test, dev, production, staging, testing2 and so on.
I want gem 'puma' to be installed for all environments except test. How can I make it?
The simplest way is to make whitelist:
gem 'puma', group: [:development, :staging, :dev, :production, :testing2]
But this seems too bad, because it'll be better just to make:
gem 'puma', except: [:test]
Is there any way to do it?

Sorry, there is no way to do this.

Related

rails ignores Gemfile

My Gemfile looks like this:
source 'https://rubygems.org'
ruby '2.2.2'
gem 'rails', '~> 4.2.1'
# a bunch of stuff that is commented out goes here
group :production do
# Use Postgres as the database for Active Record
gem 'pg', '~> 0.18.1'
# An irrelevant comment
gem 'rails_12factor', '~> 0.0.3'
# Use Puma as the server
gem 'puma', '~> 2.11.2'
end
When I run rails by typing rails server -e development, I see that it is running Puma, even though Puma is not specified for my development environment. If comment out the line that says gem 'puma', '~> 2.11.2', then WEBrick is used (as expected.)
Why is Puma used in the development environment, even when it is not specified as such in the gemfile?
Ten minutes after asking this question, I found this answer which suggested that using bundle install --without production would fix the issue, and it did. I'm going to leave this question here in case anyone else has a similar issue.

Ruby on Rails - Installing Gemfiles

I am trying to learn Ruby on Rails through this online tutorial
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
I'm creating an app demo_app for their second chapter.
This is what my gemfile currently looks like
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.2'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
this is what it's supposed to look like
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.4'
group :development do
gem 'sqlite3', '1.3.8'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
I don't understand why my gemfile looks so different.
I have the updated versions of rails, ruby and gemfile.
I even ran the commands
bundle install --without production
bundle update
bundle install
and my gemfile still looks like my first snippet of code.
I've been reading through chapters 1 and 2 of this tutorial but can't figure this out. Am I supposed to edit Gemfile in the text editor? I've already tried that and I got a hundred error messages.
How do I install the gemfile so it looks like the code in the second snippet?
Please help
Let's look through the gemfile item by item (Note that any of the numbers after gems are versions and it isn't necessary that your versions match his. You can specify them if you'd feel more comfortable but unless you come to some bug that needs it, you should be fine without.)
source 'https://rubygems.org' - You both have this and this is where the gem, bundle, etc. commands in the console get your gems from for installation.
ruby '2.0.0' - He has this and you don't. What is this doing? It's specifying the version of ruby that he's using in his rails app. You can do this if you want but it won't be necessary unless you've got multiple versions of ruby installed. Perhaps you're using RVM (Ruby Version Manager) in which case this will probably be necessary. Make sure it's the version you have by using ruby -v in the console to check your version. (Output will look something like this: ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0] where in the gemfile you can leave everything off besides the 2.1.1
#ruby-gemset=railstutorial_rails_4_0 - This is a comment in his gemfile for his own use. Likely he has multiple gem files and this helps him know which one to use when making the tutorial.
gem 'rails', '4.0.4' - You both have that which is just the version of rails you're using.
Next we have
group :development do
gem 'sqlite3', '1.3.8'
end
This is somewhat different than yours but how come? First of all the group :development do means that we only want to "do" (read: use) the gems in this block when we're in the development group. This one can be more clearly called an environment and can be configured/found in the config/environments/ folder. This is useful for having different gems and settings when running the rails server in development or test or production mode. He has the sqlite3 gem in the development group because he wants to use rails's default database gem for the tutorial. You will notice that you have the sqlite3 gem as well which means you can follow his tutorial successfully.
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
You have all these gems in your gemfile as well so no problems there. (They may be with different versions and with comments above them so that you know why each gem is there which is good. Rails puts the comments in by default for clarity in coding.)
group :doc do
gem 'sdoc', '0.3.20', require: false
end
Same as yours and likewise used for the docs group. It means that it won't be used unless you're specifically looking for it.
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
From person experience, I know that these are two gems that are required to deploy an app in production to heroku (a free hosting site) which you'll cover in chapter 1.4. You do not have them because you don't need them until you deploy to heroku. The production group again specifies that you want these gems for when the server runs in production (as it will on heroku). The pg gem is for postgres which is a database alternative to sqlite3 and the rails_12factor is something that enhances 12factor app handling. More can be learned here but it is only necessary to know that heroku requires it to host a rails app.
All the gems in yours that aren't in his and are commented out in yours are old rails standards that are left in because plenty of people still use them and they aren't truly phased out yet. They will not be necessary for the tutorial and you can delete them if you'd like.
Sorry it was long. Hope this helps your understanding.
It looks like your going through Hartl's tutorial, it's a great tutorial, however, if this is your first time coding and or using an MVC framework, everything is going to look like gibberish. Although this is bad practice moving forward, I suggest copying the entire gemfile that Hartl provides into your gemfile and then do a bundle install.
The point of this tutorial for beginners is to get through it with a BASIC understanding of how everything works. It's going to go over a lot of concepts that your not necessarily going to understand or use right away. The best thing to do is power through the best you can and try to finish the app. I finished it in 3 weeks and was more confused than ever, things only started to sync in once I started experimenting on my own and using the tutorial and other ruby/rails docs as a reference.
Programming is hard, and if this is your first foray into application development, I suggest having a basic understanding of Ruby first (http://www.codecademy.com/tracks/ruby) or even learning a scripting language like python (http://www.learnpython.org/). Learning python basics for some odd reason helped me understand ruby better, which made it easier to navigate and understand all the components of rails.
Anyway good luck and stick with it, there are tons of online resources to get you where you need to go. You just have to keep digging.

Rails tutorial 3.6, 'bin' is not recognized as an internal or external command

I'm stuck at section 3.6 of railstutorial.org, specifically when I run
> bin/rspec spec/
on my Windows 7 machine I get 'bin' is not recognized as an internal or external command.
I have already typed
> bundle --binstubs
I don't have RVM on here. Should I install it?
When I go cd bin and execute rspec, I get
'load': cannot load such file
My gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.13'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.5'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.2'
group :test do
gem 'capybara', '1.1.2'
end
group :production do
gem 'pg', '0.12.2'
end
Thank you in advance.
Just thought I'd reiterate the author's warning about this section:
"This section should only be attempted by fairly advanced users and
can be skipped without loss of continuity. Among other things, this
material is likely to go out of date faster than the rest of the
tutorial, so you shouldn’t expect everything on your system to match
the examples exactly, and you may have to Google around to get
everything to work."
As a side note... I originally tried to use ruby on rails on a windows machine but I found that much of the support community used apple or linux boxes. I have been much happier since I started dual-booting into Ubuntu. The transition was easier than I expected and I have had fewer issues with gems.

How can I get reverse a misstep--turning on https on localhost with force_ssl = true in Ruby on Rails

I can't seem to reverse a problem I created on my development machine. Luckily that was in a VM with some saved snapshots so I was able to go back to an earlier snapshot and reproduce the problem exactly in its simplest form. And, by going back to an earlier snapshot and just not repeating the misstep, I've worked around the issue, but the fact that I can't undo the change directly either suggests there is some bug, or I don't understand something critical which I'd like to.
So here's the scenario:
1) My Rails App works fine. I am on apache, a fully up to date Ubuntu 11.04 with postgresql for the db. Three things (a,b,c) that may be relevant:
a) It has this GEM file:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
gem 'bootstrap-sass', '2.0.0'
gem 'pg', '0.12.2'
gem 'bcrypt-ruby', '3.0.1'
gem 'jquery-rails', '2.0.0'
group :development, :test do
gem 'rspec-rails', '2.10.0'
gem 'guard-rspec', '0.5.5'
end
gem 'annotate', '~> 2.4.1.beta', group: :development
group :test do
gem 'rspec-rails', '2.10.0'
gem 'capybara', '1.1.2'
gem 'rb-inotify', '0.8.8'
gem 'libnotify', '0.5.9'
gem 'guard-spork', '0.3.2'
gem 'spork', '0.9.0'
gem 'factory_girl_rails', '1.4.0'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
b) It has this file in /etc/apache2/sites-available/sample_app (FYI, yes I am working through the tutorial on http://ruby.railstutorial.org/ruby-on-rails-tutorial-book though with apache and postgresql)
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
RailsEnv development
DocumentRoot /home/USER/railsProjects/sample_app/public
</VirtualHost>
and this virtual host is the only site enabled.
c) it has this line uncommented in my rails project
config.force_ssl = true
NOTE: I haven't done anything special to configure apache to use SSL.
2) In the /etc/apache2/sites-available/sample_app file, change the line
from:
RailsEnv development
to:
RailsEnv production
save, then restart apache from the commandd line with:
sudo service apache2 restart
then go to localhost in the browser. In my case it doesn't work. That's fine--fixing that isn't my question. My question is, how do I reverse this change.
3) The obvious answer would be to try this: Go back to /etc/apache2/sites-available/sample_app file and change line
from:
RailsEnv production
back to:
RailsEnv development
save and then restart apache again.
Unfortunately, that doesn't work. It seems at this point my system is left in what seems to be an unfixable state. Every time I go to
http://localhost
I am forwarded to
https://localhost
I have reproduced this behaviour several times and I cannot find any change between any of the files in /etc/ or in my railsproject between a working system where I never made the change, and a non-working system where I changed the environment to production and then changed it back.
I'm hoping someone who really understands this stuff can help a newbie out on this one. Where do I go to remove the setting that is causing http: to forward to https?

Check for gems with local :path before deploy

For some Rails applications, I'd like to have a safe-guard when I deploy to check if I have some gems configured to be looked up at a local path.
A little bit of context may help to understand.
When I'm in development mode, I want to have a gem in "local mode". In my Gemfile it is configured like this : gem 'my_gem', '~> 0.9', :path => './path/to/my_gem'.
In production, I want to be like this : gem 'my_gem', '~> 0.9', :git => 'git#git.example.com:my_gem.git'.
I've tried to make a shell script (or function) to read the Gemfile.lock and exit with an error if the gem is in "local mode".
My deployment scripts could use this to abort if I've forgotten to switch back to the proper mode.
Any help will be appreciated.
Thanks
Use
group :development do
gem 'my_gem_for_development', '~> 0.9', :require => './path/to/my_gem/lib/my_gem.rb' , :path => './path/to/my_gem/lib'
end
group :production do
gem 'my_gem', '~> 0.9', :git => 'git#git.example.com:my_gem.git'
end
Is this a gem that you're developing? Why not just write the gem to look at the rails env and change settings based on that. Then you can one canonical version of the gem and you won't have to worry about checking to see which gem version you're using. Otherwise, bor1s' solution will work just fine.

Resources