Skip to content

Instantly share code, notes, and snippets.

I'm in the process of adding AJAX to a Rails app. One obstacle I have is updating the flash. I need all my AJAX requests to update the flash if my controller sets the flash with a message that I want shown on my page. So far I've converted 4 actions to AJAX, but there are at least 10 more. Customizing each action to update the flash message gets very tedious. So I need a way of DRYing it up.

For my templates, I'm using js.erb extensions (e.g. a request to /articles/create.js would render the app/views/articles/create.js.erb template). At the top of each template, I put some javascript code with some helper code:

If you're using AJAX in Rails 3 with jQuery, there's a little gotcha you may want to know about, especially if your'e at the laundromat.

I was working at the laundromat and getting this really weird error. Every remote link was broken. Whenever I clicked on a link that was supposed to do some AJAX magic, I would get a routing error that the url wasn't being matched. Looking at the log, it was requesting with GET instead of PUT, POST, etc. I looked at the HTML source and everything looked fine:

data-remote="true" "data-method="put"

I ran into this problem today using RSpec with Webrat. I had this piece of code:

it 'should redirect to somewhere' do
  visit page_that_redirects_somewhere_path
  response.should redirect_to(somewhere_else_path)
end

I know that the controller is redirecting, but for some reason, in the test, the response was not showing a redirect.

I'm starting a JavaScript project using the Jasmine Ruby gem for testing, and I only want to use CoffeeScript. After looking around to see how other projects did it, I didn't find any project with a "pure" CoffeeScript solution. The projects I saw only had the source code written in CoffeeScript but tests written in JavaScript. I wanted all my code including tests to be written in CoffeeScript.

This is what the basic directory tree looks like in my project:

~models
 file1.coffee

(TLDR: http://railscasts.com/episodes/327-minitest-with-rails)

I just started a new Rails 3.1 project. I decided to use MiniTest::Spec for all my testing. I also wanted to use my typical testing tools: Capybara, Spork, etc. I didn't find much support for MiniTest, and even less for MiniTest::Spec. Here's some documentation on what I ended up with.

@ordinaryzelig
ordinaryzelig / gist:4709056
Created February 4, 2013 19:41
Booking url process
  • user clicks purchase
  • fare_id is sent to SearchService
  • SS queues BotQueen with fare_id
  • BQ gets job
  • BQ uses SearchClient to request data from SS (segment, fare, etc.)
  • SS handles this request, queries db, responds to BQ with results
  • BQ sends info to bot
  • bot constructs HTML, making requests to agency if necessary
  • bot returns HTML to BQ
  • BQ sends HTML to be stored in Redis
Run options: --seed 40919
# Running tests:
EE.E.E.E..E.......E
Fabulous tests in 2.850542s, 6.6654 tests/s, 7.3670 assertions/s.
1) Error:
test_0001_crawls, parses, and returns array with data of journeys(DeBahnApi::.find):
@ordinaryzelig
ordinaryzelig / service.rb
Created January 21, 2013 14:16
after moving things to middleware
send(route.fetch(:http_method).downcase, url) {
if (handler)
respond_with_json_protocol do |protocol|
handler.add_to_response(params, protocol)
end
else
erb route.fetch(:name)
end
end
[
{
"nominees": [
{
"film": "Amour",
"nominee": "Nominees to be determined"
},
{
"film": "Argo",
"nominee": "Grant Heslov, Ben Affleck and George Clooney, Producers"

I just released a gem called minispec-metadata which allows you to add metadata to specs similar to RSpec. It can be useful to use with Capybara so you can easily switch drivers on a per-spec basis.

describe 'Usage', some: 'metadata' do

  before do
    # Example usefulness:
    # Capybara.current_driver = metadata[:driver]
  end

 it 'defines a metadata method', more: 'metadata' do