JSONP - Wikipedia
Under the same origin policy, a web page served from server1.example.com cannot normally connect to or communicate with a server other than server1.example.com. An exception is the HTML <script> element. Taking advantage of the open policy for <script> elements, some pages use them to retrieve Javascript code that operates on dynamically-generated JSON-formatted data from other origins. This usage pattern is known as JSONP. Requests for JSONP retrieve not JSON, but arbitrary JavaScript code.
Web Operations 101 For Developers
This post is not about devops, it's not about lean startups, it's not about web scale, it's not about the cloud, and it's not about continuous deployment. This post is about you, the developer who's main purpose in life has always been to build great web applications. In a pretty traditional world you write code, you write tests for it, you deploy, and you go home. Until now.
Use Capybara on any HTML fragment or page
I think this pattern is really useful not just for upgrading suites from Webrat, but really anywhere you have an HTML fragment or string that you’d like to use Capybara’s matchers on.
Zip files with Ruby
When you need to zip up files in Ruby, use zipruby
.
sudo gem install zipruby
You can add existing files, add files from strings and even add directories.
Example usage:
require 'zipruby'
cars = %w[audi bmw mercedes]
zipfile = Tempfile.new('my.zip', 'tmp')
Zip::Archive.open(zipfile.path, Zip::CREATE) do |zip|
zip.add_file '/tmp/me.txt'
zip.add_dir 'cars'
cars.each do |car|
zip.add_buffer "cars/#{car}.txt", "This #{car} is mine!"
end
end
Credits go to winebarrel for the Ruby bin...
Downloading files from Ruby on Rails
To offer files for download, use send_file
.
def download(file)
send_file file.path, :disposition => 'attachment'
end
Note that a send_file
replaces the default :render
action.
Vim: Scroll up/down, keeping your cursor in its row
If you are in the middle of a file and want to scroll, but don't want to move your cursor all the way to the top row, use this:
One line
-
Ctrl+Y
→ Move viewport down -
Ctrl+E
→ Move viewport up (Extra lines)
Half a screen
-
Ctrl+U
→ Move viewport Up -
Ctrl+D
→ Move viewport Down
Those are the default key bindings, they may be different for other layouts/behaviors (like mswin
).
Read everything about scrolling in vim in the linked vimdoc.
Thunderbird Addon: Message Archive Options
The addon lets you set the format of the dates used for the year and month folders that the Archive function in Thunderbird 3+ creates. It exposes the (hidden) preference that lets you change the granularity of the folders to either a single folder; an archive folder and a year subfolder; or a month also.
Additionally the addon can add modifiers to the 'A' key shortcut so the key is Shift + A (or Control or Alt).
Hide your Selenium browser window with a VNC server
This is now part of geordi. Please don't follow the instructions below, if you use geordi.
Inspired by the recent headless Selenium note, I found yet another solution for the problem to hide your selenium tests away.
This has the advantages
^
- not to require a gem (so you do not force this on others)
- to allow you to take a look at the running webdriver if necessary
Simply make a script th...
Passenger booting Rails 3 application in wrong environment
Passenger gives you the possibility to define in which environment your app should be started.
This has to be added to the VirtualHost configuration for Apache for Rails 2 applications:
RailsEnv development
When running Rails 3, you need
RackEnv development
Firefox 3.6 truncates long tables when printing
Possible fixes:
- Upgrade your Firefox. It's fixed in 5.0.
- Hunt down funny
float
oroverflow
directives in your CSS. - Remove
<h1>
and<caption>
tags in proximity of your table (seriously).
How to look at hidden X screens
When you have a program running in a hidden X screen (like with Xvfb for Selenium tests) you may want to look at that hidden screen occasionally.
First, find out what X displays are currently active:
netstat -nlp | grep X11
This should give you some results like these:
unix 2 [ ACC ] STREAM LISTENING 8029600 4086/Xvfb /tmp/.X11-unix/X99
unix 2 [ ACC ] STREAM LISTENING 8616 - ...
simple_format helper for Javascript
The Javascript code below is a rough equivalent to the simple_format helper that ships with Rails:
function simpleFormat(str) {
str = str.replace(/\r\n?/, "\n");
str = $.trim(str);
if (str.length > 0) {
str = str.replace(/\n\n+/g, '</p><p>');
str = str.replace(/\n/g, '<br />');
str = '<p>' + str + '</p>';
}
return str;
}
Unlike the Rails helper, this does not preserve whitespace. You probably don't care.
Resque: Work off queues manually
If you're writing a spec for an application using Resque, you may need to work off queues manually without having an external worker running.
For this, you could use ResqueSpec which basically stubs away Resque completely.
If you don't want that, but more closely mimic what actually happens, use this instead:
module Resque
def self.work_off(*queues)
if queues.any? { |queue| peek(queue) }
worker = Work...
Resque: Clearance authentication for dashboard
Resque comes with its own dashboard (Resque server) that you can mount inside your Rails 3 application with
#config/routes.rb:
require 'resque/server'
My::Application.routes.draw do
# ...
mount Resque::Server => '/resque'
end
Unfortunately, since this bypasses the filters in your ApplicationController
, everyone can access this dashboard now (unless you have some Rack-based authentication in place, like Devise).
If you're using ...
How to hide your selenium browser window with "headless"
Note: While the solution in this card should still work, we prefer another solution now: Hide your Selenium browser window with a VNC server.
If you would like to hide the annoying selenium browser window that always gets the focus and prevents you from working, you can use the headless gem. This note provides some instructions how you can get it to work with your cucumber accepta...
A few hints when upgrading to Ruby 1.9
Note: If you are currently working with Ruby 1.8.7 or 1.9.3, we recommend to upgrade to Ruby 2.1 first. From our experience this upgrade is much smoother than the jump from 2.1 and 2.2, while still giving your the huge performance gains of Ruby 2. Also, if you're on Ruby 1.8.7, we recommend to skip a troublesome upgrade to 1.9.3 and go straight to 2.1.
When trying to make a Rails app run on Ruby 1.9, you're likely to encounter several issues. Here are a few solutions (obviously not exhaustive):
When running `bundle ...
Change file permissions with Ruby
You can say
File.chmod(0604, '/path/to/file') # rw----r--
The leading zero is meaningful.
Delete a Clearance session after some time of inactivity
This note describes how to kick a user out of a Rails application after she hasn't requested an action for a while. Note that this is different from deleting sessions some time after the last login, which is the default.
Also note that this is probably a bad idea. Most sites keep sessions alive forever because having to sign in again and again is quite inconvenient for users and makes your conversion rates go down the toilet. [The Clearance default is to keep sessions around for one year](https://makandracards.com/makandra/701-when-sessio...
Rails 3: Sending tempfiles for download
When you create a temporary file (e.g. to store a generated Excel sheet) and try to send it to the browser from a controller, it won't work by default. Take this controller action:
class FoosController < ApplicationController
def download
file = Tempfile.new('foo')
file.puts 'foo'
file.close
send_file file.path
end
end
Accessing this controller action will usually raise a 404 not found in the browser and the Apache log will say:
The given path was above the root path: xsendfile: ...
How to type accented characters on keyboard layouts without dead keys
Ubuntu comes with keyboard layouts like "Germany Eliminate Dead Keys", which are practical for programming.
If you need to type accented characters with such a layout, make sure to configure a Compose key. You can then look up which compose combo will produce the character you need.
E.g. you can type "á" by pressing Compose, ´, a
.
Rails 3: Make "link_to :remote => true" replace HTML elements with jQuery
In Rails 2, you could use link_to_remote ... :update => 'id'
to automatically replace the content of $('#id')
.
To do the same in Rails 3, include usual rails-ujs JavaScript, and put this into your application.js
:
$(function() {
$('[data-remote][data-replace]')
.data('type', 'html')
.live('ajax:success', function(event, data) {
var $this = $(this);
$($this.data('replace')).html(data);
$this.trigger('ajax:replaced');...
Fix for: Your Firefox profile cannot be loaded. It may be missing or inaccessible.
If you use Selenium and Launchy to open web pages, you might run into an error saying "Your Firefox profile cannot be loaded. It may be missing or inaccessible.".
This happens because Launchy tries to use your Firefox running the web driver to open the page.
For Launchy < 0.4.x, as a workaround you can set an environment variable
export LAUNCHY_BROWSER=`which google-chrome`
For newer Launchys you also need to set:
export BROWSER=`which google-chrome`
If you want to do this in a ruby script, you can say
chrome_path ...
RSpec 2.6 supports "any_instance" now
This finally works:
User.any_instance.should_receive(...)
as does
User.any_instance.stub(...)
Note: You won't have RSpec 2.6 if you're still working on Rails 2.
Failtale
Free Hoptoad/Airbrake alternative which can capture exceptions from any platform. It comes with a Rails notifier and a RESTful API to write your own notifiers for Javascript, etc.
Should we some day wish to do away with the current way we deal with exceptions, this might be a good place to start.