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.
HTML elements with
overflow-y: auto or
overflow-y: scroll will get a scrollbar when their content is higher than their own height.
When you scroll an element , the element's
scrollTop property is updated with the scrollbar's new position. You can also set
element.scrollTop = 30 to scroll the element to a vertical pixel position counted from the top.
The browser's main document viewport is also scrollable by default. The element that …
This card tries to summarize by example the different uses of heredoc.
strip_heredoc should be used for a text, where you want to preserve newlines. (multi-line -> multi-line)
squish should be used for a text, where you want to squish newlines. (multi-line -> one-line)
def foo bar = <<~TEXT line1 line2 line3 TEXT puts bar.inspect end foo => "line1\nline2\nline3\n"
Read more: [Unindent HEREDOCs in Ruby 2.3](/makan…
In order to save the original value of a constant, set the new value and restore the old value after a scenario was completed, you can use the following helper. It takes care of saving the old constant value, setting the new one without throwing warnings and resets the value with an
This module also enables you to introduce new global constants.
Since these newly defined constants do not have any value to be reset to,
they simply are deleted (
remove_const) once the respective Cucumber step finishes.
You can copy the file at…
Set the ruby version in
.ruby-version to 2.3.5, then perform these steps one by one, fixing errors as they occur:
1) Update gems as listed below, and bundle
2) Boot a Rails console - see below for a list of changes you will probably need
3) Run Specs with
4) Run Cucumber features (with Geordi's
5) When all tests are green, look through your Gemfile and remove as many version constraints as possible.
6) Boot the application in different environements to spot further issues, e…
Imagine you have 2 HTML boxes. The first one has a
margin-bottom of let's say
30px and the second one a
20px. After rules of collapsing margins have been applied we have a margin of
50px) between these two boxes (see this article for an illustration) . This is because no addition of both margins takes place but the maximu…
Using uncountable resources is not recommended as it breaks Rails' magic, e.g. when using
form_for. You'll always be better off using simple pluralizable resources.
Rails automatically creates path names for routes defined via the
resource method. When you put
resource 'user' into
config/routes.rb, you can call
users_path and get the path to the
index action in the
However, if you have an uncountable resource like
Sheep, you cannot access the index action via
sheep_path, because it will…
When using virtual attributes, the attached trait can be useful to automatically copy errors from one attribute to another.
Here is a typical use case where Paperclip creates a virtual attribute
:attachment, but there are validations on both
:attachment_file_name. If the form has a file picker on
:attachment, you would like to highlight it with errors from any attribute:
class Note < ActiveRecord::Base has_attached_file :attachment validates_attachment_presenc...
In ruby you can easily read and write CSVs with the standard CSV library class.
On top of this, you can use the gem smarter_csv for reading (not writing) CSVs in a more comfortable way:
Here is an example…
When you generate a URL in a mailer view, ActionMailer will raise an error unless you previously configured it which hostname to use. Configuring the correct hostname is quite annoying when you have multiple deployment targets with different hostnames, e.g. a staging server and a production server.
Using the hack below you don't need to configure a default hostname for your mailers. They will always use the hostname for the current request:
class ApplicationController < ActionController::Base before_filter :make_action_maile...
Next time you have to do more than trivial CSS changes on a project, you probably want to have live CSS reloading, so every time you safe your css, the browser updates automatically. It's pretty easy to set up and will safe you a lot of time in the long run. It will also instantly reload changes to your html views.
Simply follow the instructions below, taken from blog.55minutes.com.