I believe that when WEBrick has trouble bringing up your Rails application, the WEBrick component that is supposed to print you a pretty error message has a bug and sometimes...

...pointed to the actual problem. Possible causes discovered by looking at the logs A Rails plugin would require the sqlite3 gem, but that gem wasn't in the Gem bundle...

...which requires ApplicationController when loaded. When your ApplicationController requires later-loaded gems when loaded, Rails will not boot. Here is an example for an ApplicationController requiring Aegis::Controller from the...

...In this case aegis must be listed before resource_controller in your Gemfile or Rails will not boot...

...all will run all RSpec examples and Cucumber features. The report will be written RAILS_ROOT/coverage/index.html. Here is what the task does in detail: Generates aggregated coverage of both RSpec...

Works with Rails 2 and Rails 3 Reports for app/**/*.rb and nothing else If called with an environment variable IGNORE_SHARED_TRAITS=true it ignores Modularity traits...

...you need to make a static web page and find yourself missing all your Rails comforts, take a look at StaticMatic. This works like an extremely stripped down version of...

...Rails, giving you HAML SASS helpers partials When done, everything is simply compiled to static HTML and CSS, so no need to install anything on your server. If you receive...

...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...

Note: In Rails 3+ you can use Array.wrap instead of the solution here. In the past you could use Array(...) or #to_a to turn an object into an array...

...you can copy the attached file to config/initializers or require it from a non-Rails project. You can now say: 5.listify # => [5] [5].listify # => [5] Note that this method no...

RSpec 1 (Rails 2) With the most recent spec_candy.rb helpers you can say: User.stub_any_instance(:foo => :bar) user = User.new user.foo # => :bar RSpec 2 (Rails 3) RSpec 2 comes with...

...with the following snippet: actors = movie.actors actors.build actors.unshift(actors.pop(1)) # won't work with Rails 4+ Because build_for_form creates new objects and appends them to the one-to...

...many association collection object you only have to reorder the collection objects. Sorting with Rails 3+ = form.fields_for :children, @parent.children.sort_by(&:name) do |fieldsform...

...look at the filter chain in specs. You can do it like that on Rails 2: controller.class.filter_chain.map(&:method) Note that we need to look at the controller's class since...

...objects to scope down to only some of them: controller.class.filter_chain.select(&:before?).map(&:method) For Rails 3, do it like this (warning, ugly!): controller._process_action_callbacks.select { |c| c.kind == :before }.map(&:filter)

...by calling #dup on it: user = User.find(3) user.freeze unfrozen_user = user.dup Notes for Rails 2 users There is a bug in Rails 2.3.x where duping an ActiveRecord instance...

...just upgraded to Bundler 10.0.10 you might get the following error when bringing up Rails: /usr/lib/ruby/1.9.1/psych.rb:148:in `parse': couldn't parse YAML at line 17 column 14 (Psych::SyntaxError...

...This is caused by Rails localization files (en.yml, de.yml, etc.) using symbols for various translation strings, and Bundler 10.0.10 defaults to a new YAML engine which cannot handle symbols.

Once Rails knows a given string is html_safe, it will never escape it. However, there may be times when you still need to escape it. Examples are some safe...

...to turn your string into an unsafe string to get the escaping love from Rails: embed = javascript_tag('var foo = 1337;') # This is an html_safe SafeBuffer embed.to_str # This...

...add ANSI color annotations (which your console then interprets). To print a log (e.g. rails log) and color all lines containing "FATAL" in red and all lines with "INFO" in...

...revert to the original Rubygems by saying gem uninstall slimgems #If you are using Rails 2.3.11 Rails 2.3.11 can also trigger this warning. Install 2.3.12+. After installation, you need to...

...or from the public folder with javascript_include_tag. The subtle difference that tells rails how to build the path correctly is a single slash at the beginning of the...

...stylesheet in /assets you need to add it to the list of assets that Rails will precompile...

makandra dev

...want to uninstall newer ones): bundle _1.0.10_ -v Bundler version 1.0.10 An example is rails 3.2, which freezes bundler at version ~> 1.0: Bundler could not find compatible versions for gem...

In Gemfile: rails (~> 3.2) was resolved to 3.2.0, which depends on bundler (~> 1.0) Current Bundler version: bundler (1.13.6) You can solve this with: gem install bundler -v 1.0.10

...files at once. To activate this feature, set the multiple attribute: Or in a Rails view: <%= file_field_tag "images[]", multiple: true %> This works in IE10+. Make sure that the...

...incoming files into an array. Obviously this naming convention is not compatible with default Rails nested attribute setters, so you'll need to write a form model to adapt...

...code below is a rough equivalent to the simple_format helper that ships with Rails: function simpleFormat(str) { str = str.replace(/\r\n?/, "\n"); str = $.trim(str); if (str.length > 0) {

...str.replace(/\n\n+/g, ' '); str = str.replace(/\n/g, ' '); str = ' ' + str + ' '; } return str; } Unlike the Rails helper, this does not preserve whitespace. You probably don't care...

JavaScripts and CSS should be minified for production use. In Rails 3.1+ the asset pipeline will take care of this. Thus you're best off using an uncompressed version of...

...be easier and you will still get all the minification love once deployed. In Rails 2.3 and 3.0 you should at least embed external JavaScript libraries in minified form, using...

mitrev.net

...false (intercepts invocation), whereas false&.class permits the invocation. &. might also remind you of Rails' Object#try. However, their scopes are different, and in Rails 4 nil-checking is only...

...for when you can use transactions: Transactions cannot be used for Selenium features, where Rails and test run in different processes and thus don't see changes within the transaction...

...different cleaning strategies I measured the runtime of different strategies using an average-sized Rails project (with MySQL): Cucumber RSpec Transaction 87.14, 86.65 10.20, 10.11

...route that only responds to a given format, here is how you do it: Rails 3 match 'sitemap.xml' => 'feeds#sitemap', :constraints => { :format => 'xml' }, :as => 'sitemap' Rails 2 map.sitemap 'sitemap.xml', :controller...

...port and the username (default root) and password (default msandbox). When working on a Rails project, you need to paste all of that into your config/database.yml. Connecting to a sandbox...

Rails has the handy controller method send_file which lets us download files easily. We can decide whether the file should be downloaded (disposition: 'attachment') or shown in the browser...