The caveat is that you also have to move all other script tags from your views to the bottom of the page. This can be done with helpers.
There are two distinct ways of commenting Haml markup: HTML and Ruby.
This will create an HTML comment that will be sent to the client (aka browser):
/= link_to 'Example', 'www.example.com' # => <!-- = link_to 'Example', 'www.example.com' -->
Only use this variant if you need the comment to appear in the HTML.
This will comment code so it will not be sent to the client:
-# = link_to 'foo'
99% of the time you'll be adding notes for other developers, or disabling code sections that should…
Many mail clients do not support external style sheets. Some even require 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 files and do the inlining for you.
Since Roadie is now in passive maintenance mode, we go with premailer:
Include premailer in your Gemfile:
This is a short overview of things that are required to upgrade a project from the Asset Pipeline to Webpacker. Expect this upgrade to take a few days even the diff is quite small afterwards.
1. Find all libraries that are bundled with the asset pipeline. You can check the
application.js and the
import statements. The source of a library is most often a gem or a vendor directory.
2. Find an working example for each library in the application and write it down.
3. Find out the ver…
In Rails, we usually have a mailer setup like this:
class MyMailer < ActionMailer::Base def newsletter mail to: 'email@example.com', from: 'firstname.lastname@example.org', subject: 'My mail' end end
If you want to preview your mail in the browser, you can use the Action Mailer Preview. To inspect the mail directly in your email client, just create an
.eml file and open it with your client:
mail = MyMailer.newsletter
We generally use multiple application servers (at least two) and you have to search on all of them if you don't know which one handled the request you are looking for.
Rails application logs usually live in
Web server logs usually live in
You can use
grep in this directory to only search the latest logs or
zgrep to also search older (already zipped) logs.
zgrep is used just like
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.