In rails, we usually have a mailer setup like this:
class MyMailer < ActionMailer::Base def newsletter mail to: 'firstname.lastname@example.org, from: 'email@example.com', subject: 'My mail' end end
If you want to preview your mail in the browser, you can use the Action Mailer Preview. If you want to inspect your mail directly in your email client, just create an
.eml file and open it with your client:
mail = MyMailer.newsletter…
All our projects have enum-like requirements like this:
Most of the time, this requirement is also needed:
In our past projects there are many different solutions for these related requirements, e.g.
ChoiceTrait, methods like `available_…
Starting with Rails 3.2, there is a nicer way to display custom error pages (i.e. "page not found") that does not interfere with ExceptionNotifier. (I'm not sure about Airbrake et al.)
José Valim has posted this on his blog, here is a slightly more verbose rundown:
Register your own app as the application responsible for showing error pages by adding
config.exceptions_app = self.routes
When we want to use our own (or bought) fonts in an application with Webpack(er), we have two options. We can
The first option turns out to be straightforward: Import the stylesheets in the index.js of the pack you're using:
From at least Rails 4, the ActionView tag helper turns
Array values of HTML options into a single space-separated string.
This means you can pass an array to
extra_classes = %w[one two]
= link_to 'Dashboard', root_path, class: ['btn', 'btn-primary', *extra_classes]
= content_tag 'div', 'Hello World', class: %w[alert alert-info]
=> <div class="alert alert-info">Hello World</div>
Webpacker is Rails' way of integrating Webpack, and version 4 has been released just a few days ago, allowing us to use Webpack 4.
I successfully upgraded an existing real-world Webpack 3 application. Below are notes on everything that I encountered.
Note that we prefer not using the Rails asset pipeline at all and serving all assets through Webpack for the sake of consistency.
There are various ways to run external commands from within Ruby, but the most powerful ones are
Open3.popen3. Since those can do almost everything you would possibly need in a clean way, I prefer to simply always use them.
Behind the scenes,
Open3 actually just uses Ruby's
spawn command, but gives you a much better API.
Basic usage is
stdout_str, error_str, status = Open3.capture3('/some/binary', 'with', 'some', 'args')
Localizing a non-trivial application can be a huge undertaking. This card will give you an overview over the many components that are affected.
When you are asked to give an estimate for the effort involved, go through the list below and check which points are covered by your requirements. Work with a developer who has done a full-app localization before and assign an hour estimate to each of these points.
appmust be translated: Screens, mailer templates, PDF templates, helpe…
Clearfix is a hack to clear floating elements without additional HTML markup.
If you only need to support IE8 and above, a great clearfix with few side effects is:
=clearfix &:after content: "" display: block clear: both
This is a Sass mixin.
You will find many clearfix solutions that clear with
display: table instead:
=clearfix &:after content: "" display: table clear: both
The problem with th…
zeroclipboard.js in some of our projects but now we switched to
clipboard.js because it does not rely on flash. Flash support of the major browsers has ended.
Some more advantages of clipboard.js:
Subject: performance on smartphones and tablets
I just used your framework in one project and must say,
I am really pleased with it – but only on a desktop computer.
Have you benchm…
Starting from Rails 4.0, you can use a special form options helper called
#collection_check_boxes. It behaves similar to
#collection_select, but instead of a single select field it renders a checkbox and a label for each item in the collection.
= form_for @post do |form| = form.collection_check_boxes :author_ids, Author.all, :id, :name_with_initial
How generated form params look like ———————————…
Carrierwave includes some handy helper methods you can use to resize your images. Here is a quick summary of them, if you need more details read the docs. You can also use all command line options from RMagick directly if these helpers are not good enough for you.
Capybara will fail to find
<a> tags that are missing an
An example would be an AngularJS application where the following HTML actually works. 
Capybara will fail to find that link, even though looking it up via the DOM shows it:
>> find_link("Hello") Capybara::ElementNotFound: Unable to find link "Hello" >> find("a").text => "Hello"
Spammers have started abusing other application to send their spam. This works like this:
In Rails 3+, you can use:
class Project delegate :url_helpers, to: 'Rails.application.routes' def project_path url_helpers.project_path(self) end end
For Rails 2, use the attached Modularity trait. It will give any Ruby class a method
#url_writer, on which you can call URL helpers:
class Foo does 'write_urls' def self.class_method url_writer.session_path end def instance_method url_writer.project_path(5) end end
sort method doesn't work as expected with German umlauts:
["Schwertner", "Schöler"].sort => ["Schwertner", "Schöler"] # you probably expected ["Schöler", "Schwertner"]
Also numbers in strings will be sorted character by character which you probably don't want:
["1", "2", "11"].sort # => ["1", "11", "2"] # you probably expected ["1", "2", "11"]
Also the sorting is case sensitive:
["a", "B"].sort # => ["B", "a"] # yo...
When building an application that sends e-mails to users, you want to avoid those e-mails from being classified as spam. Most obvious scoring issues will not be relevant to you because you are not a spammer.
However, your application must do one thing by itself: When sending HTML e-mails, you should include a plain-text body or tools like SpamAssassin will apply a significant score penalty. Here is how to do that automatically.