User.active.to_sql Rails 2 Use either the Edge Rider or fake_arel gem to get #to_sql backported to Rails 2. If you don't want to use...

...a gem for this, you can do this with vanilla Rails 2: User.active.construct_finder_sql...

...block where you define replacements (as you already know). This issue applies to both Rails 2 (with rails_xss) as well as Rails 3 applications. Here is a fix to...

...html_safe.gsub(/(f)/) { puts $1; 'b' } f => "boo" Why? This is because of the way rails_xss implements "unsafe" methods: # vendor/plugins/rails_xss/lib/rails_xss/string_ext.rb UNSAFE_STRING_METHODS = [ ..., "gsub",...

for unsafe_method in UNSAFE...

makandra dev

...Commit using a story title from Pivotal Tracker geordi con[sole] [TARGET] # Open a Rails console locally or on a Capistrano deploy target geordi cu[cumber] [FILES] # Run Cucumber features...

...PATH # Optimize .png files geordi ra[ke] TASK # Run a rake task in several Rails environments geordi re[move-executable-flags] # Remove executable-flags from files that should not be...

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

clipboardjs.com

...only of a single javascript file, so it does not trigger additional requests with rails it automagically provides user feedback by selecting the text it has copied it provides callbacks...

...modes like copying from a text field or from a data attribute How to (rails) Add clipboard.js to your project through bower or by downloading and adding the javascript file...

makandra dev

...only want to compile when needed. This card shows what will cause Webpacker (the Rails/Webpack integration) to compile your assets. When you run a dev server While development it is...

...call any helper that accesses asset paths (e.g. javascript_pack_tag or image_tag), Rails will compile your entire assets within its process before that helper returns. This will cause...

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

If you are using Angular and want something like Rails' simple_format which HTML-formats a plain-text input into paragraphs and line breaks, this directive is for you.

...class DynamicStylesheetsController < ApplicationController def show logical_path = RELATIVE_PATH_TO_YOUR_TEMPLATE path = File.join(Rails.root, logical_path) template = Sass::Rails::SassTemplate.new(path) environment = YourApp::Application.assets context = environment.context_class.new(environment, logical_path...

...these helper methods to your ApplicationController: class ApplicationController < ActionController::Base private def integration_test? Rails.env.test? && defined?(Cucumber::Rails) end def observable_redirect_to(url) if integration_test? render :text => "If...

makandra dev

The Rails asset pipeline improves delivery of application assets (javascripts, stylesheets, images, fonts). Here are some basic facts about its inner workings. No magic Manifests are the handle on your...

...results. You can do this locally using the following commands: rake assets:precompile # precompiles to Rails.root/public/assets rake assets:clobber # deletes the public/assets directory After precompilation, you can check the...

Issue: You have an app using jsbundling-rails and esbuild. After deploy, the assets built by esbuild are missing in public/assets. Solution: Add app/builds to your git repo (by adding...

...entirely find out why, and we eventually did not care). This happened on a Rails 3.2 application and is likely to happen on Rails 4 applications as well. Your best...

stackoverflow.com

To reload a single-item association in Rails 5+, call #reload_ : post.reload_author In older Railses you can say post.author(true...

If Rails or Rake are complaining about a missing gem that is listed in your Gemfile.lock and the listed version is properly installed, something is seriously wrong and needs to...

...seeing the gems it needs. Remove those wrong and unneccesary directories and Rake and Rails will boot again. To avoid this in the future, make sure everybody installs gems on...

...more unreadable to more attributes you delegate. Alternative to def_delegators When working with Rails, use delegate...

...:to =>...

class Post < ActiveRecord::Base belongs_to :topic delegate :title, :category, :to...

end Looks a lot like the above def_delegators call but behaves differently. Rails does not provide a pretty solution to this, but simply write a method for it...

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

Firefox 5.0.1, which we were using for most Rails 2.3 projects, does not run on Ubuntu 14.04 any more. Here is how to update affected projects. Update (or create) .firefox...

...run tests with Geordi, it will tell you how to install it. On a Rails 2 project: Update your Cucumber-related gems as described in Upgrading Cucumber and Capybara, including...

makandra dev
rakeroutes.com

...print, if present requires interactive_editor, if present (basically makes vim available) loads .irbrc_rails if in Rails (e.g. for the Rails console) .irbrc_rails defines efind(email), which is...

...to find users by email requires hirb, if present set the prompt to your Rails app's name Of course you can put anything into these files...

...all styling inline, which means you'll have to do your styling inline. For Rails applications, you can use Roadie or premailer, which lets you keep your well-structured CSS...

...passive maintenance mode, we go with premailer: Include premailer in your Gemfile: gem 'premailer-rails' In your ApplicationMailer define the mailer layout: layout 'mailer' To get the mailer stylesheet (and...

Ubuntu 12.04 LTS x64, Ruby 1.8.7, Rails 2.13, PDFKit 0.5.4, Phusion Passenger Apache 2 I ran into this, when I started using passenger to deal with the Single Thread Issue...

...wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf sudo chmod +x /usr/local/bin/wkhtmltopdf Tell PDFKit where to find your wkhtmltopdf: Rails::Initializer.run do |config| require "pdfkit" config.middleware.use PDFKit::Middleware PDFKit.configure do |config| config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf' # <-- the fix...

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

...following helper. def greeting message = ''.html_safe message << 'Welcome to ' message << content_tag(:span, Rails.env, class: 'greeting--location') content_tag :div, message, class: 'greeting' end That looks clumsy and is...

...say something like this? def greeting render_haml <<-HAML .greeting Welcome to %span.greeting--location = Rails.env HAML end It would be, and you can have it, too. You simply need to...

makandra dev

...like to split a Ruby array into pairs of two, you can use the Rails ActiveSupport method in_groups_of: >> [1, 2, 3, 4].in_groups_of...

...split a Ruby array into a given number of groups, you can use the Rails ActiveSupport method in_groups: >> [1, 2, 3, 4, 5, 6].in_groups...