...groups are a useful RSpec feature. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put them. I recommend storing them like this: spec/models/shared_examples/foo.rb spec/models/shared_examples/bar.rb...
...those shared examples available to all specs, put the following into your spec_helper.rb (for rails 4 in rails_helper.rb), above the RSpec.configure block: Dir[Rails.root.join("spec/models/shared_examples/**/*.rb")].each {|f| require f...
You will need to upgrade to RSpec >= 2 and rspec-rails >= 2 for Rails 3. Here are some hints to get started: In RSpec 2 the executable is rspec, not...
RSpec and rspec-rails have been completely refactored internally. All RSpec classes have been renamed from Spec::Something to RSpec::Something. This also means that every require 'spec/something' must...
...fixing errors as they occur: Update gems as listed below, and bundle Boot a Rails console - see below for a list of changes you will probably need Run Specs with...
...constraints as possible. Boot the application in different environements to spot further issues, e.g. rails console staging Gem updates Replace ruby-debug with byebug or pry Replace mysql with mysql2...
Spring is a Rails application preloader. When debugging e.g. the rails gem, you'll be wondering why your raise, puts or debugger debugging statements have no effect. That's because...
...you can only write environment variables that subprocesses will see. For tests with a Rails application (i.e., that call rails or rake or other binstubbed commands), this method may help...
...following project: ruby -v ruby 1.8.7 bundler -v Bundler version 1.13.7 gem -v 1.8.30 rails -v Rails 3.2.22.1 Running specs or features resulted in: uninitialized constant Gem::LOADED_SPECS_MUTEX...
...previous settings described in Maximum version of Rubygems and Bundler for Ruby 1.8.7 and Rails 2.3 (even the rails version was rails 3.2 and not 2.3) seems not to work...
Expiration of Rails sessions By default Rails sessions expire when the user closes her browser window. To change this edit your config/initializers/session_store.rb like this: ActionController::Base.session = { :key => '...', :secret => '...' :expire_after...
} In older Railses the initializer is not available. Set the option in the environment.rb instead: config.action_controller.session = { :key => '...', :secret => '...' :expire_after => 10.years } Expiration of Rails cookies In addition to the...
...Note.all.preload(:attachments).ids.size # => 8 Note I created a bug report for this in the Rails project: https://github.com/rails/rails/issues/46455
...want something similar, so we define our own helper object: let :helper do Spec::Rails::Example::HelperExampleGroup::HelperObject.new.tap do |helper| helper.send :extend, LayoutHelper end end it 'should work' helper.title('Hello...
helper.instance_variable_get('@content_for_title').should == 'Hello World' end This applies to Rails 2. Maybe RSpec 2 / Rails 3 are smarter...
...or controller) logic. Here is how. Note: this has only been tested on a Rails 2 application. It should work similarly for Rails 3. Put this into your ApplicationController:
Now you can use with_full_urls in views, helpers or controllers and Rails methods like url_for will generate "full" URLs that have a protocol and hostname. You...
Next, configure your application to use that middleware by putting this inside your Rails initializer block (config/environment.rb for Rails 2, config/application.rb for Rails 3): require 'lib/rack/cookie_stripper.rb' config.middleware.use Rack::CookieStripper...
You will get this when you are using the latest version of Rails with a recent version of Rack: SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
...future versions will even invalidate your existing user cookies. The warning is caused by Rails calling Rack incorrectly. It is unclear when this is going to be fixed in Rails...
You may disable multi-line autocomplete by using the --nomultiline commandline switch (for Rails, use bin/rails console -- --nomultiline), or specifying IRB.conf[:USE_MULTILINE] = false in your ~/.irbrc.
There is a nasty bug in all version of Rails 2 and some versions of Rails 3.x where two chained scopes with hash conditions on the same attribute would...
...what a user may see or change. Workaround If you are using an affected Rails version and cannot switch to a fixed version, you can use this manual workaround.
...for that. The reason why you never had to write this line is that Rails does this for you when it boots the environment. That also means that if you...
...have an embedded Rails app in your spec folder (like has_defaults), and you boot its environment, it should call Bundler.require for you and you don't need to require...
...part is a bit lengthy, but it allows you to debug strange bugs with Rails autoloading the wrong constants. Due to the constant lookup rules above, Rails sometimes guesses wrong...
...in development and production, but the problem exist for both environment. Development During development, Rails unloads all classes after every request. This way code changes take effect immediately, without requiring...
...can remove conditions, order, etc by using the unscope method. It is available on Rails 4+. Examples Consider an exemplary User class as follows. For the examples below, we will...
...scope of any constraints. users.unscoped ^ SELECT "users".* FROM "users" While unscope was introduced with Rails 4, the unscoped method has been around since Rails...
...to append filepaths to it. With this method, Ruby code can look like this: Rails.root/"features"/"fixtures"/"picture.jpg" Alternatively you can use the #join method, which feels less magic: Rails.root.join...
...and The...
...field should have the error...
...steps now have built-in support for Rails and Bootstrap (v3-v5) error classes. When using Bootstrap, it is no longer necessary to...
...performance due to the cost of reindexing. Minidusen is currently compatible with MySQL, PostgreSQL, Rails 3.2, Rails 4.2 and Rails 5.0. Basic Usage Our example will be a simple address...
...Note that #origin_class it roughly equivalent to the blockless form of #unscoped from Rails 3.2+, but it works consistently across all Rails versions. #unscoped does not exist for Rails...
...and is broken in Rails...
...behaves like an unordered Hash, but is much faster. Info If you're using Rails you already have concurrent-ruby in your bundle. hamster hamster provides several collection classes, such...
There is a practical short list for valid/invalid example email addresses - Thanks to Florian L.! The definition for valid emails...
Submit buttons in Rails come with a useful option :disable_with which will disable the button when clicked and change its label to something like "Please wait...". An annoying side...
...is to re-enable the submit button before leaving the page. This works in Rails 3: $(window).unload(function() { $.rails.enableFormElements($($.rails.formSubmitSelector...
t.column_name, :string t.remove :first_name t.remove :last_name t.rename :cool, :awesome end Rails 2 caveats Rails 2 does not understand the extra options hash but seems to build...
...only created when you rename columns. For other operations such as add and remove Rails 2 will still give you single ALTER statements. More See RobinWu's APIdock comment for...