Fix Capistrano warnings: Missing public directories
I got these warnings while deploying a Rails 3.2 app with asset pipeline enabled:
*** [err :: host.tld] find: `/opt/www/hollyapp.com/releases/20120503115342/public/images': No such file or directory
*** [err :: host.tld] find: `/opt/www/hollyapp.com/releases/20120503115342/public/stylesheets': No such file or directory
*** [err :: host.tld] find: `/opt/www/hollyapp.com/releases/20120503115342/public/javascripts': No such file or directory
Folders like public/javascripts
might not exist if you're using the asset pipeline (...
Scroll a textarea to a given line with jQuery
You can use this code:
function scrollToLine($textarea, lineNumber) {
var lineHeight = parseInt($textarea.css('line-height'));
$textarea.scrollTop(lineNumber * lineHeight);
}
Some caveats about this code:
- Your textarea needs to have a line-height in Pixels.
- The code will scroll to the line number in the text area, not the line number of the original text. These will differ if any lines wrap at the edge of the textarea.
Also see our solution for [scrolling a textarea to a given position with jQuery](htt...
Convert primitive Ruby structures into Javascript
Controller responses often include Javascript code that contains values from Ruby variables. E.g. you want to call a Javascript function foo(...)
with the argument stored in the Ruby variable @foo
. You can do this by using ERB tags (<%= ruby_expression %>
) or, in Haml, interpolation syntax (#{ruby_expression}
).
In any case you will take care of proper quoting and escaping of quotes, line feeds, etc. A convenient way to do this is to use Object#json
, which is defined for Ruby strings, numb...
Git: Change the text editor used to enter commit messages
In your ~/.gitconfig
:
[core]
editor=nano
Uncaught exception in PassengerServer client thread exception: Cannot accept new connection: Too many open files
So you probably see the following error trace within your Passenger log file if you got here:
[ pid=123 thr=1401414 file=ext/nginx/HelperAgent.cpp:964 time=2012-04-27 10:01:49.273 ]: Uncaught exception in PassengerServer client thread:
exception: Cannot accept new connection: Too many open files (24)
backtrace:
in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:429)
in 'void Client::threadMain()' (HelperAgent.cpp:953)
You can either
- Lower the maximum number of running Passenge...
zClip :: jQuery ZeroClipboard
zClip is a lightweight jQuery "copy to clipboard" plugin built using the popular Zero Clipboard library. This plugin uses an invisible Adobe Flash movie that is fully compatible with Flash Player 10 and below.
fnando/i18n-js
A small library to provide the Rails I18n translations in Javascript clients.
CodeMirror
CodeMirror is a JavaScript component that provides a code editor in the browser. When a mode is available for the language you are coding in, it will color your code, and optionally help with indentation.
xterm: Have a black background by default
xterm
by default uses black text on white background.
To change that to something like "light gray on black", do a
vim ~/.Xresources
...and put this in there:
xterm*background: black
xterm*foreground: lightgray
Afterwards, feed these changes to your current X session:
xrdb ~/.Xresources
Any subsequent xterm
will be colored white on black background.
Hint:
- While you're at it, you can also set xterm's font in your
~/.Xresources
file. Just addxterm*faceName: monospace:pixelsize=14
. - If you don't want to chan...
How to fix Passenger "Unexpected end-of-file detected" error
This is for you if Passenger gives you the following useless error message.
Passenger encountered the following error:\
The application spawner server exited unexpectedly: Unexpected end-of-file detected.Exception class:
: PhusionPassenger::Rack::ApplicationSpawner::Error
Most often this happens because you are missing a gem. Usually Passenger would tell you about that but in some cases it can't.
To resolve this issue, run:
bundle install
If this does not do the trick for you, take a look at the Apache log files for de...
Validate attachment presence using paperclip
Make sure you call the methods in the following order and not vice versa:
has_attached_file :image
validates_attachment_presence :image
Validation with condition works fine, too:
validates_attachment_presence :image, :if => :method
This is because validates_attachment_presence
is only available after saying has_attached_file
.
LibreOffice Calc: How to paste cells without overwriting other cells
The default behavior of Calc is that when you paste cells from the clipboard, it overwrites whatever cells occupy the same space on the spreadsheet. If you would like to insert the cells in the clipboard instead by pushing the existing cells to the bottom or the right, you can do it like this:
- Go to Edit / Paste Special
- The paste dialog has a segment Shift Cells. Choose Down or Right depending on what you want to do.
Building Gem 'RedCloth' with Bundler and GCC 4.6
If you cannot install the gem 'RedCloth' via bundle install you might want to try the following.
Specifying extra build options for bundler which will only be applied when building RedCloth:
bundle config build.RedCloth --with-cflags=-w
Issue with an escaped "equals" sign in the development log email representation
An =
is represented by =3D
in the log file.
I got confused by the leading 3D
which is not part of the token, you can see in the following example:
Hallo Max!
Danke f=C3=BCr deinen Beitrag. Bitte best=C3=A4tige deine Teilnahme, inde=
m du auf den folgenden Link klickst:
http://localhost:3000/beitraege/13/confirm?token=3DEA7BB9DA73FF17AO
Viele Gr=C3=BC=C3=9Fe vom Team!
My token is actually only EA7BB9DA73FF17AO
.
Shell script to deploy changes to production and not shoot yourself in the foot
Geordi, our collection of command line tools, has been extended by another command deploy-to-production
. This script encapsulates the following workflow:
- Pull the production branch.
- Show which commits from the master would make it to production with this deploy.
- Ask if you want to proceed.
- If yes, merge the master into the production branch, push and deploy with
bundle exec cap production deploy:migrations
The script will ask you for the names of your master branch, production branch an...
Move page breaks in LibreOffice Calc
- You can define what will be printed by checking View -> Preview Page Breaks and then moving around the blue borders.
- You can now define the size of the printed content under Format -> Page -> Sheet -> Scale. You can choose between three scaling modes which all suck in their own way.
Fix warning: Cucumber-rails required outside of env.rb
After installing Bundler 1.1 you will get the following warning when running tests:
WARNING: Cucumber-rails required outside of env.rb. The rest of loading is being defered until env.rb is called.\
To avoid this warning, move 'gem cucumber-rails' under only group :test in your Gemfile
The warning is misleading because it has nothing to do with moving cucumber-rails
into a :test
group. Instead you need to change your Gemfile
to say:
gem 'cucumber-rails', :require => false
Todo.txt: Future-proof task tracking in a file you control
Solution to share a todo.txt
file over Dropbox and make it editable on Android und iOS devices. Also comes with a CLI interface for common actions like adding a new task or listing all tasks pertaining to a GTD context.
Unfortunately it only supports flat todo lists, no nesting.
validates_acceptance_of is skipped when the attribute is nil
validates_acceptance_of :terms
only works if terms
is set to a value. The validation is skipped silently when terms
is nil
.
While this behavior is useful to validate acceptance in the frontend and not the admin backend, it also makes it very easy to unintentionally skip the validation altogether by forgetting to add the checkbox to a form. E.g. validates_acceptance_of :terms_with_typo
will be skipped silently even if there is no column with t...
Git: Push a new branch and track it immediately
When you create a new branch and push it to origin
, you won't be tracking it. This means a git pull
won't know its remote version.
You could use difficult commands to set up a branch's tracking but it's easier to just push it like this:
git push -u
From the documentation on git push:
-u, --set-upstream
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<nam...
Define a route that only responds to a specific format
You won't usually have to do this. It's OK to route all formats to a controller, and let the controller decide to which format it wants to respond.
Should you for some reason want to define a route that only responds to a given format, here is how you do it:
Rails 3
match 'sitemap.xml' => 'feeds#sitemap', :constraints => { :format => 'xml' }, :as => 'sitemap'
Rails 2
map.sitemap 'sitemap.xml', :controller => 'feeds', :action => 'sitemap', :format => 'xml'
MySQL shell: Vertical vs horizontal layout
When talking to your MySQL server via a mysql
shell, you can terminate queries by ;
or \G
-- the latter gives you a vertical output.
You know this:
mysql> SELECT * FROM users;
+----+---------+---------------------+-----------------+
| id | name | email | greeting |
+----+---------+---------------------+-----------------+
| 1 | Alice | alice@example.com | Hello world! |
| 2 | Bob | bob@example.com | Hello universe! |
| 3 | Charlie | charlie@example.com | Hi mom! ...
Tabs in Textarea Plugin for jQuery
This is a demo of the "Tabby" Javascript jQuery plugin to use tabs in regular textareas to make them suitable for in-browser coding of languages like HTML, CSS, Javascript, or your favorite server-side language. The idea is to be able to use a press of the TAB button or SHIFT+TAB to indent or outdent your code.
Ruby: Find the most common string from an array
This will give you the string that appears most often in an array:
names = %w[ foo foo bar bar bar baz ]
names.group_by(&:to_s).values.max_by(&:size).try(:first)
=> "bar"
This is very similar to the linked StackOverflow thread, but does not break on empty arrays.
Note that try
is provided by ActiveSupport (Rails). You could explicitly load activesupport
or use andand
on plain Ruby.