If your Carrierwave uploader dynamically generates the filename (e.g. by incorporating a user's name), you must call
model.save! after recreating versions, because
uploader.recreate_versions! does not update the model with the stored filename.
As your Rails project grows, you will accumulate a number of small patches. These will usually fix a bug in a gem, or add a method to core classes.
Instead of putting many files into
config/initializers, I recommend to group them by gem in
Enumerable.find(&block), which returns the first item in the collection for which the block evaluates to
first_post_with_image = posts.find do |post| post.image end
However, sometimes it's not the item you're interested in, but some value depening on it – e.g. the value the block evaluated to. You could first map the collection and then take the first truthy value, but this way you need to process the whole collection twice:
first_image_url = posts.map(&:image).find(&:present?).url
If the mapping …
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…
Pour color on your Rails console with awesome_print. Turn confusing long strings into formatted output. Have objects and classes laid out clearly whenever you need it.
gem 'awesome_print', :group => :development into your Gemfile. Now on the Rails console you have the command
ap that will give you a colored, formatted output of whatever you pass it. See the example output of the
User class below.
For customization visit the repository on Github.
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.
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…
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...
When building a web application, one is tempted to claim it "done" too early. Make sure you check this list.
Desktops, tablets and mobile devices have all different screen resolutions. Does your design work on each of them?
Occasionally, your designer will hand you designs where elements break the layout's horizontal container width, like navigation buttons of a slider that should be at the left/right of the browser window, or simply by applying a background color that reaches until the left and right of the browser window.
In the past, we've done some horrible things to achieve that. Like
margin: 0 -10000px plus
There is a much saner approach.
Consider the following markup:
First, find the element in question.
let element = document.querySelector('.my-element') // or $('.my-element').get(0) when using jQuery
getComputedStyle. It takes an optional 2nd argument to filter for pseudo elements.
let style = window.getComputedStyle(element, '::before')
let color = style.getPropertyValue('background-color…
# Given the following models class Image < ActiveRecord::Base has_many :album_image has_many :albums, through: :album_images end class Album < ActiveRecord::Base has_many :album_image has_many :images, through: :album_images end # Join model class AlbumImage < ActiveRecord::Base belongs_to :album belongs_to :image end
Destroying a record in this setup will only remove the record itself, and leave orphaned join records behind.
image = Image.last
image.destroy # removes only the
Ubuntu has decided to disable PDF processing because ImageMagick and the underlying Ghostscript had several security issues.
When your Ghostscript is up to date (and receiving updates regularly), you can safely reactivate PDF processing on your computer like this:
<!-- disable ghostscript format types -->
If you need it enabled for an…
draw.io is a web application that lets you draw flow charts and other diagrams. It works much nicer than office suite tools like LibreOffice Draw.
Its UI is easy to use: Simply drag and drop shapes or arrows from the left-hand side onto your sheet. Drag start/end of arrows to shapes to automatically connect them. You can add text to shapes by selecting them and typing. To add colors, select a shape and use the right-hand panel.
While you can, you don't need to store your charts online. "Offline" storage can be files…
In this example we assume that not only the storage gem changes but also the file structure on disc.
Part A: Create a commit which includes a script that allows you to copy the existing file to the new file structure.
Part B: Create a commit which removes all paperclip logic and replace it with the same code you used in the first commit
Here are some implementation details you might want to reuse:
Google Tag Manager (GTM) is a related tool, but on a higher level and thus with much more power. GTM is not a replacement for GA. Rather, it can make GA configurable without changing anything in the application's code base (and much more beyond, see below).
Only prefer GTM if the customer requests it, or if he is updating his tracking r…
Safari on iOS accepts an
apple-touch-icon favicon that is used for stuff like desktop bookmarks. Always define a solid background color for them.
If you use PNGs with a transparent background, Safari will use just set a black background on your pretty icon. This is almost never what you want.
You can fix that by applying a white background via ImageMagick like this:
For webpages to load fast it's recommended to optimize images. Ideally an image's file size should be as small as possible while still being of decent quality. This card demonstrates two command line tools for image optimization
identify to fetch information about pictures.
convert can change size/quality and strip meta information. Both commands are supplied by ImageMagick.
$ identify in.jpg
in.jpg JPEG 294x440 294x440+0+0 8-bit sRGB 92.8KB 0.000u 0:00.000
$ convert in.jpg -resize x220 -strip -quality 85 out.jpg