K.R.C. LogoThe Book of Kara

EVE on Rails - Creating an EVE in-game-optimised version of your Rails site

Published 14 February 2008

Hi! You've stumbled upon a blog post by a guy named Ryan. I'm not that guy anymore, but I've left his posts around because cool URIs don't change and to remind me how much I've learned and grown over time.

Ryan was a well-meaning but naïve and priviledged person. His views don't necessarily represent the views of anyone.

One thing I love about being a Rails hobbyist is that I can continually think about the best way to do things. With no paycheck hinging on a deliverable, I can refactor continuously until I'm convinced I have the best code I can muster. Sure, I never get anything done, but the exercise makes me a better programmer.

I mentioned previously how to set up an EVE Online-focused rails site. After reading through old Riding Rails posts, I noticed this excellent post on iPhone-optimising your rails project. This process is almost exactly the same for sharing views between EVE and a normal browser. In Rails 2.0 all you have to do is register the MIME Type.

config/initializers/mime_types.rb:

Mime::Type.register_alias "text/html", :eve

Then, in your application controller, use a before_filter to adjust the format as necessary.

app/controllers/application_controller.rb:

before_filter :isolate_eve_browser

private
  # EVE-specific data
  def isolate_eve_browser
    if eve?
      request.format = :eve
      request_trust unless trusted?
      end
    end
  end

Now everywhere you want to separate your EVE views from your standard browser views, simply use respond_to.

app/controllers/home_controller.rb:

class HomeController < ApplicationController
  def index
    respond_to do |format|
      format.html
      format.eve
    end
  end  
end

You can then use app/views/home/index.html.erb and app/views/home/index.eve.erb respectively. This means you can also request trust on the appliaction level instead of on the action level as I mentioned previously. Notice that request_trust method above? It should look something like:

app/controllers/application_controller.rb:

private
  def request_trust
    response.headers["eve.trustme"] = (
      "http://#{request.env['HTTP_HOST']}/" +
      "::Your custom message begging for trust."
    )
    render :template => 'trust_me'
  end

The action is rounded out by creating app/views/trustme.eve.erb, which is the template rendered above when the user declines to trust you. Happy coding!

K.R.C. LogoPrevious: Setting up A Rails Application for EVE OnlineNext: Doublethinking version targeting