How to change MySQL's data directory
-
Have a backup.
-
Stop MySQL:
sudo service mysql stop
-
Move (or copy) your
mysql
directory. If you want/mnt/mysql
to be the new directory, do it like this:sudo mv /var/lib/mysql /mnt/
-
Open your MySQL configuration (
sudo vim /etc/mysql/my.cnf
) and change thedatadir
value to your new path (e.g./mnt/mysql
) -
Modify your AppArmor configuration:
sudo vim /etc/apparmor.d/usr.sbin.mysqld
Change/copy the lines granting access to
/var/lib/mysql
to your new path. Otherwise MySQL will not ...
HTML5 and Web Video: Questions for the Industry from the Community
What are Google’s plans for turning WebM into a genuinely open standard, one that is based on consensus like the rest of W3C’s HTML5 effort? Would Google fully support such an effort? Even the WebM project’s domain is controlled by Google. Google chose to release WebM under the Creative Commons license which would theoretically allow a standards body to use the specification as a basis for a truly open standard. Would Google agree to adopt the specification and changes that would emerge from an open process in a timely and robust manner? Wha...
Speed up file downloads with Rails, Apache and X-Sendfile
When you use the send_file
method to send a local file to the browser, you can save resources on the application server by setting the :x_sendfile
option to true
. This option is activated by default for Rails 3, so you need to understand this.
What this option does is not to send any data at all, but rather set the local file path as a new response header:
X-Sendfile: /opt/www/awesome-project/shared/downloads/image.png
When the response comes back from Rails to...
Perform HTTP basic authentication in Cucumber (with or without Selenium)
This card describes a Cucumber step that lets you say:
When I perform basic authentication as "username/password" and I visit the admin area
The path component ("... the admin area") is parsed through your path_to
helper in features/support/paths.rb
.
Capybara
The step definition is part of Spreewald. The step has been tested with multiple versions of Capybara, Rack::Test and Selenium.
Webrat (legacy)
This is a simpler version of the step above:
When /...
Access the documentation of all locally installed gems
In case https://www.rubydoc.info/ is to slow or offline, you can also read a gem documentation offline.
Start a server with gem server
and go to http://0.0.0.0:8808/
. Here you will find a list of all installed gems and it is possible to navigate to the documentation if installed e.g. http://0.0.0.0:8808/doc_root/rubocop-0.77.0/
In case you set the configured RubyGems to not install documentation by default, you need to add generate the documentation for the specific gem.
gem install rubocop --document
`...
ETags with memcached
I love ETags, but there’s something that annoys me: most implementations revolve around pulling a record out of a data store and only “rendering” the response if it hasn’t been modified.
The problem with this approach is that request has already gone through most of your application stack–parsing params, authentication, authorization, a few database lookups–so ETags are only saving you render time and some bandwidth.
While working on a Sinatra-based JSON web service that gets very heavy traffic, I wanted to find a way to short-circuit...
Concurrency issues with find-as-you-type boxes
Find-as-you-type boxes are usually built by observing changes in a text field, and querying the server via AJAX for search results or suggestions when the field has changed.
A common problem with this implementation is that there is no guarantee that AJAX responses are evaluated in the same order as the original requests. The effect for the user is that the search results are flashing back and forth while the user is typing the query, and when the user has stopped typing the last results don't always match the final query.
Workarounds
----...
Restrict Apache access to your local computer
When you are using Apache for development, it still accepts connections from everyone in the same network as you.
In order to only allow requests to your Apache coming from your local computer, edit your /etc/apache2/ports.conf
so all Listen
directives point to 127.0.0.1
:
Listen 127.0.0.1:80
<IfModule mod_ssl.c>
Listen 127.0.0.1:443
</IfModule>
After the change stop and start your Apache and check with netstat
that Apache no longer listens to 0.0.0.0
:
sudo /etc/init.d/apache2 stop
sudo /etc/init.d/ap...
Apache: Log the original client IP when your site sits behind a reverse proxy
When your site is mapped into the URL-space of another server using mod_proxy
, ProxyPass
and ProxyPassReverse
, all requests in your Apache logs are logged with the IP address of the proxying server. The IP address of the original client doing the request is not logged, making it difficult to trace problems and run statistics.
Short answer
There is no easy way to fix this. Use the log of the proxying server instead, which logs the original client IPs you're looking for.
Long answer
You can fix this for your ac...
Apache: Redirect all requests from one host to another
In order to redirect all requests from redirecting-host.com
to desired-host.com
while keeping path and query params unchanged, change your Apache VHost to something like this:
ServerName desired-host.com
ServerAlias redirecting-host.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^desired-host.com$
RewriteRule ^.*$ http://desired-host.com%{REQUEST_URI} [R=301,L]
Take care to keep all those ^
, $
and !
as seen in the example.
How to fix failing controller specs 91% of the time
If your controller spec never reaches your controller code:
-
Make sure you are signed in.
-
Make sure you are actually triggering a request by saying
get :edit
or something siliar. -
Know that views are not rendered by default for controller specs unless you tell them to (
render_views
).
^
describe UsersController do
describe '#edit' do
it 'should work' do
sign_in
get :edit
end
end
enddefine something like this in your spec_helper.rb:
def sign_in(user = User....
Use the back button in Cucumber
In order to go back one page in your Cucumber tests, you can use the following step definition for Capybara:
When(/^I go back$/) do
visit page.driver.request.env['HTTP_REFERER']
end
If you're on Webrat, this should work:
When(/^I go back$/) do
visit request.env["HTTP_REFERER"])
end
An improved version of this step is now part of our gem spreewald on Github.
Useful collection of Sass mixins
This collection of Sass mixins enables cross-browser styling (including IE with CSS3PIE) with less lines of code.
This enables PIE for IE up to version 8 only (the first part is not possible in Haml, so use ERB):
<!--[if !IE]><!-->
<%= stylesheet_link_tag 'screen', :media => 'screen' %>
<!--<![endif]-->
<!--[if lte IE 8]>
<%= stylesheet_link_tag 'screen_with_pie', :media => 'screen' %>
<![endif]-->
These would be your two screen Sasses:
# screen_with_pie.sass
...
Bookmarklet to generate a commit message with Pivotal Tracker story ID and title
For clarity and traceability, your commit messages should include the ID and title of the Pivotal Tracker story you're working on. For example:
[#12345] Add Google Maps to user profiles
Optional further commit messages in the body
Also see Howto: Write a proper git commit message
To quickly generate such commit messages, add a new link "Commit" to your bookmarks and use the following Javascript as the link URL:
javascript:(function() { ...
Aliases for routes
The following initializer provides an :alias => "my_route_name"
option to restful routes in your route.rb
. This simply makes the same route also available under a different ..._path / ..._url helpers.
For example,
map.resources :notes, :alias => :snippets
Gives you
notes_path, notes_url, new_note_path... #as always
snippets_path, snippets_url, new_snippet_path... #from the alias
Put this into an initializer:
Share your Internet connection under Ubuntu
This note describes how to setup a box running Ubuntu to share its Internet connection with another PC.
- You will need two network interfaces to do this. One will connect to the Internet (e.g. UMTS card), one will share the connection with the other PC (e.g. Wired LAN).
- On the Ubuntu PC, right-click on the connection symbol in the tray. Select Edit connections.
- Switch to the wired LAN card. Go to Edit and set the method to Shared to other computers.
- Connect the other PC to your wired LAN card. You can use the same cable that wo...
Request a non-HTML format in controller specs
If a controller action responds to other formats than HTML (XML, PDF, Excel, JSON, ...), you can reach that code in a controller spec like this:
describe UsersController do
describe '#index' do
it 'should be able to send an excel file' do
# stubs and expectations go here
get :index, :format => 'xls'
end
end
end
Remember that both the :format
parameter and the HTTP_ACCEPT
header can m...
Get the current layout's name in a view or partial
This returns the name (including path) of your current layout:
response.layout
=> "layouts/admin" # inside views that are using the 'admin' layout
You most likely do not need the full path, so go ahead and do this:
File.basename(response.layout)
=> "admin"
Generate a path or URL string from an array of route components
When using form_for
you can give the form's target URL either as a string or an array:
form_for(admin_user_path(@user)) do ... end
# same as:
form_for([:admin, @user]) do ... end
Same for link_to:
link_to("Label", edit_admin_user_path(@user))
# same as
link_to("Label", [:edit, :admin, @user])
polymorphic_path
and polymorphic_url
If you would like to generate a path or URL string from an array of route components just as form_for
does, you can use polymorphic_path
or polymorphic_url
:
polymorphic...
MySQL: Disable query cache for database profiling
If you want to see how long your database queries actually take, you need to disable MySQL's query cache. This can be done globally by logging into a database console, run
SET GLOBAL query_cache_type=OFF;
and restart your rails server.
You can also disable the cache on a per query basis by saying
SELECT SQL_NO_CACHE * FROM ...
You also probably want to disable Rails internal (per-request) cache. For this, wrap your code with a call to ActiveRecord::Base.uncached
. For example, as an around_filter
:
d...
An obscure kernel feature to get more info about dying processes
This post will describe how I stumbled upon a code path in the Linux kernel which allows external programs to be launched when a core dump is about to happen. I provide a link to a short and ugly Ruby script which captures a faulting process, runs gdb to get a backtrace (and other information), captures the core dump, and then generates a notification email.
Generate a strong secret from the shell
A good tool to generate strong passwords and secrets is "apg". You can get it with
sudo apt-get install apg
To create a strong secret for sessions, hashed Paperclip paths, etc. say
apg -m128 -a1 -E\'\"
Arguments explained:
- The
-m
parameter defines the secret length -
-a1
makes apg choose from all 7-bit ASCII characters instead of just the alphabet -
-E\'\"
excludes quote characters so you can easily paste the secret into a Ru...
Reload the page in your Cucumber features
Both these approaches will keep your GET
parameters -- and will only work for GET
requests.
-
Capybara:
When /^I reload the page$/ do visit [ current_path, page.driver.request.env['QUERY_STRING'] ].reject(&:blank?).join('?') end
-
Webrat:
When /^I reload the page$/ do visit url_for(request.params) end
For a step that distinguishes between drivers (Selenium, Rack::Test, Culerity), check [n4k3d.com](http://n4k3d.com/blog/2011/02/02/reloading-the-page-in-cucumber-with-capybara-and-seleniu...
A quick guide to pull requests
It’s pretty common for projects hosted on GitHub to receive “pull requests”: requests from people who have cloned your project, made a modification to it and then asking you to merge their changes back into the main project.
There are a lot of ways you can handle these pull requests, here are some of them.