Code example for implementing Cross-Origin Resource Sharing (CORS) in Rails.
Cross-Origin Resource Sharing (CORS) is a browser technology specification, which defines ways for a web service to provide interfaces for sandboxed scripts coming from a different domain under same origin policy. CORS is a modern alternative to the JSONP pattern. While JSONP supports only the GET request method, CORS also supports other types of HTTP requests. Using CORS enables a web programmer to use regular XMLHttpRequest which supports better error handling than JSONP. On the other hand, JSONP works on legacy browsers that do not have C...
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.
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.
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.
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...
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.
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:
Ctrl+Y
→ Move viewport downCtrl+E
→ Move viewport up (Extra lines)Ctrl+U
→ Move viewport UpCtrl+D
→ Move viewport DownThose 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.
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).
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
^
Simply make a script th...
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
Possible fixes:
float
or overflow
directives in your CSS.<h1>
and <caption>
tags in proximity of your table (seriously).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 - ...
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.
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 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 ...
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...
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):
You can say
File.chmod(0604, '/path/to/file') # rw----r--
The leading zero is meaningful.
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...
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: ...
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
.
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');...
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 ...