Gemfile styling - ruby-on-rails

I would like to know if there is a generally accepted style on Gemfile.
I generate new rails application, open the Gemifile, and add all the gems I need under appropriate group (test, dev etc.).
When I look at Rails opensource projects, most are not structured this way. They have the standard installed Rails gems left as they are or commented out, and then there is a second list of gems under it for the gems that the app actually needs. For me, this is less readable, but is there a reason why a lot of people seem to do it this way?

There is no general standard or consensus, exactly like there is no general standard on leaving commented code in a Ruby file.
Generally, the idea of leaving stuff commented out is a bad habit from the days where we were used to code without a version control system.
If you are not using a Gem, there is no need to leave it there commented out. It's fine to comment it out temporarily while testing different behaviours with/without or temporarily for whatever reason, but once you reached a decision to take out the gem it's safe to remove it.
Personally, I generally structure the Gemfiles in the following way:
source 'https://rubygems.org'
# Rails core gems at the top
gem 'rake'
gem 'rails', '4.2.5'
gem 'sass-rails', '~> 5.0'
.
.
.
gem 'responders', '~> 2.0'
# Gems, in alphabetical order
gem 'yajl-ruby', '~> 1.2.0', require: 'yajl'
gem 'bcrypt', '~> 3.1.0', require: 'bcrypt'
.
.
.
# Environment dependent gems, in alphabetical order
group :test do
gem 'database_cleaner', require: false
.
.
end
group :development do
gem 'letter_opener', '~> 1.4.0'
.
.
.
end
group :production do
gem 'clockwork', '~> 1.2.0', require: false
.
.
.
end
group :development, :test do
gem 'byebug', require: false
.
.
.
end

Related

Better Errors Gem not working in local browser, no errors visible

I added the Better Errors gem to my gemfile like as seen in my gemfile below, and ran bundle and saw Using better_errors 1.1.0 and restarted my server several times. I watched the railscast episode on how to install it. I’ve never had a problem installing any other gem in the past (I'm new to programming). I read the documentation and I already checked for this:
Note: If you discover that Better Errors isn't working - particularly after upgrading from version 0.5.0 or less - be sure to set config.consider_all_requests_local = true in config/environments/development.rb.
Any ideas on how to get this gem working would be much appreciated! Here is my gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.5'
group :development, :test do
gem 'rspec-rails'
gem 'capybara'
end
# Use sqlite3 as the database for Active Record
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development do
gem 'sqlite3'
gem 'better_errors'
end
gem 'bootstrap-sass', '~> 3.1.1'
# 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'
With Vagrant, add this to your app's config/environments/development.rb (anywhere inside the configure block):
BetterErrors::Middleware.allow_ip! "0.0.0.0/0"
Then restart your server.
(This is just a slight variation on Sasha's solution.)
DO NOT add this to your production environment!
Valerie -- are you on a virtual machine? Better errors can sometimes not work well with VMs.
The solution I've found is this:
First, in your app's config/environments/development.rb (anywhere inside the configure do), add:
BetterErrors::Middleware.allow_ip! ENV['TRUSTED_IP'] if ENV['TRUSTED_IP']
Then you need to define that environment variable. Find your remote IP by firing up a browser, hitting the old error page (just throw a raise in a controller or something), and finding the "REMOTE_ADDR" in the error page's "Show env dump" section. Then copy that IP and set it as an ENV variable (in your .env or application.yml file, or wherever you keep those).
Note -- DO NOT add that to production. It's unnecessary at best (Better Errors should only be run/included in development -- as you've ensured above).
Then restart your server. Any chance that fixes it?
in addition for all better you need to add this to your config/environments/development.rb:
BetterErrors::Middleware.allow_ip! "TRUSTED_IP"
where "trusted_ip" is "REMOTE_ADDR" in default error page
for me it's 10.0.2.2
In the file app/config/environments/development.rb do you have this line present in the code ?
# Show full error reports and disable caching.
config.consider_all_requests_local = true
I am running vagrant, rails 5 and ruby 2.3 and I added the below to my config/environments/development.rb and got it working.
# Allow usage of better_errors on Vagrant
BetterErrors::Middleware.allow_ip! "10.0.2.2"
# Show full error reports and disable caching.
config.consider_all_requests_local = true
Same answers as above but just wanted to confirm it for anyone running the rails 5 beta.
Old question but in case this can help anyone. I just ran into the same issue running this on my localhost: Better errors was installed and configured but I received no helpful errors.
Issue was easily solved by running bundle update.
This updated Using better_errors 2.9.1 (was 2.4.0) and now I get better errors.

Rails - New temp directory getting automatically created every time I run bundler command

I have generated a basic web app using Rails 4.1.4/Ember.js and everything works as expected, but every time I run 'bundle install' etc. it creates a new (empty) folder in my root directory of the form 'bundler20140904-22128-1jtt7b7' (the random digits at the end change each time). They pile up so deleting them is getting kind of annoying, but does anyone have any ideas why this could be happening??. I couldn't find any info via Google, but I was consulting this tutorial if that helps. I'm running CentOS 6.5 with RVM and I have no idea which files are relevant but here's my application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module MyProject
class Application < Rails::Application
end
end
config.ru:
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/ra
ils/spring
gem 'spring', group: :development
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
#EMBER
gem 'ember-rails', git: 'git://github.com/emberjs/ember-rails.git'
If you need any more info please let me know. I'd be grateful for any help you can provide!
Hey everyone thanks for the help. I ended up re-generating the app from scratch, and the problem went away. As far as what was causing this, it looks like for some reason the line:
gem 'ember-rails', git: 'git://github.com/emberjs/ember-rails.git'
in my Gemfile was causing problems. I changed it to:
gem 'ember-rails'
and it was fixed.

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.

How should I format my guard file to watch all files apart from log files?

Guard's really starting to tick me off now. Thought I had a nice regex going to select all files in my project apart from the ones with an extension of 'log':
Guardfile :
guard :rspec do
watch(%r{^.+$(?<!\.log)})
end
Rubular permalink:
http://rubular.com/r/qPyeSs54BF
But this results in nothing. I save ANY file, and guard just sits there like a lemon. So annoying.
Same when I do this:
guard :rspec do
watch(%r{^.+$})
end
Rubular permalink:
http://rubular.com/r/UxFXyq9lRm
this is what really gets me. As you can see from the rubular links, it's perfectly valid regex, but Guard doesn't listen to it. When can't it just take in regular regex? What's with the stupid %r{} crap? And I wish Guard didn't try to be clever, running individual specs when I tweak them, and then running entire spec files when I tweak those.
Is there some over-arching configuration somewhere where I can set guard to run all specs when any file other than a log file is updated and nothing else? Such a sensible thing to want to do and it's been a complete nightmare.
Here are my Gems:
source 'https://rubygems.org'
group :development do
gem 'capistrano'
gem 'guard-rspec'
gem 'guard-livereload', require: false
gem 'rb-fsevent'
gem 'debugger'
end
group :development, :test do
gem 'rspec-rails', '~> 2.14.0'
gem 'sqlite3'
end
group :test do
gem 'factory_girl_rails'
gem 'capybara', '~> 2.2.0'
gem 'selenium-webdriver'
# uses a program called 'libqtwebkit-dev' to build. To install 'libqtwebkit-dev' in Ubuntu, run
# sudo apt-get install libqtwebkit-dev
# gem 'capybara-webkit'
gem 'rb-readline'
gem 'launchy'
gem 'database_cleaner'
end
group :production do
gem 'pg'
end
# standard library
gem 'rails', '4.0.1'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
gem 'sdoc', require: false
end
# custom
gem 'devise'
gem 'puma'
UPDATE
Maybe this regex is a little better:
.+?\.(?!log).+
http://rubular.com/r/dsYKzUE8FF
So how should I go about implementing this in my guard file?
Short answer: You don't. You tell Guard to pass specific file sets (for lack of a better way to reference regex results) to specific plugins should other specific file sets be modified in any way.
For Guard to "work properly"
Guard has to be ALREADY watching the directory the Gemfile is in (although this is platform/adapter specific).
The plugin has be designed to respond to the kind of change (modified or added or deleted)
The plugin has to be designed to actually handle that file (and not ignore the change)
source: https://github.com/guard/guard/wiki/Understanding-Guard
I recommend anyone interested in using Guard visit the above link.

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.

Resources