Additional Steps for RailsInstaller in Windows 8

[UPDATE: You no longer need to do most of the steps below in recent versions of NodeJS. RailsInstaller/RubyInstaller should work just by installing NodeJS.]

If you’ve tried using RailsInstaller or RailsFTW in Windows 8, you might think it works out of the box — that is, until you open a page that uses the asset pipeline and get a cryptic message related to your CSS/JS.

The reason for this is that the built-in JScript runtime is incompatible with Rails. You need to install Node.js and make it the primary runtime for Rails to work.

The easiest way to do this is:

  1. Install Node.js
  2. Run Node.js » “Node.js command prompt”
  3. Get the path of node.exe via “path” command. You’ll get something like:
    C:\Users\bry>path
    PATH=C:\Users\bry\AppData\Roaming\npm;C:\Program Files\nodejs\;C:\Program Files...

    Get the “C:\Users\bry\AppData\Roaming\npm;C:\Program Files\nodejs\;” part.

    Tip: right click command prompt window » Poperties » tick QuickEdit Mode so you can select and right click to copy to clipboard.

  4. Edit the setup_environment.bat for RailsInstaller (e.g. C:\RailsInstaller\Ruby2.0.0\setup_environment.bat, Notepad will do), find the following line
    SET PATH=%RUBY_DIR%\bin;%RUBY_DIR%\lib\ruby\gems\1.9.1\bin;%ROOT_DIR%\DevKit\bin;%PATH%

    for RailsFTW this will be setrbvars.bat (e.g. C:\RailsFTW200402\bin\setrbvars.bat) and the line will be:

    SET PATH=%RUBY_BIN%;%PATH%

    and add the Node.js path after the SET PATH= e.g for RailsInstaller:

    SET PATH=C:\Users\bry\AppData\Roaming\npm;C:\Program Files\nodejs\;%RUBY_DIR%\bin;%RUBY_DIR%\lib\ruby\gems\1.9.1\bin;%ROOT_DIR%\DevKit\bin;%PATH%

    for RailsFTW:

    SET PATH=C:\Users\bry\AppData\Roaming\npm;C:\Program Files\nodejs\;%RUBY_BIN%;%PATH%
  5. At this point, RailsInstaller » “Command Prompt with Ruby and Rails” and RailsFTW » “Start Command Prompt with Ruby” will now have Node.js as the runtime.

Ruby on Windows

Some posts just write themselves. Today’s post comes from my reply to a guy in PhRUG who still thinks you need a Mac before you can develop Rails applications.

windows and ruby

This is probably the biggest problem the Ruby/Rails community has when trying to spread the word in this country: the lack of interest in supporting Windows.

I mean, a typical response to the legitimate question “I’m using Windows, how to I practice RoR?” is the fanboy answer: “Get a Mac!”

And that, my dear readers, is a dick move. If I was an average college student and you told me that, I’ll immediately think “WTF?!? I just want to try out this open-source language and web framework and I need to shell out a couple of years worth of tuition?!?

Answering “Format your hard drive and install Linux” is less of a dick move, but a dick move nonetheless.

Thus, if we rubyists want to spread the word about Ruby, we’ll have to make Windows a viable OS for Ruby development. Here are a few options available to us:

Continue reading “Ruby on Windows”

HTML Line Breaks in Rails Text Fields

Problem:

Rails doesn’t automatically convert newline (“\n“) characters in strings to line breaks (“<br>“) with h.

<%= h @user.address %>

Solution:

Manually replacing the \n with <br> without using h will make your site vulnerable to XSS attacks. The proper approach would be to convert the line breaks after h. Note that Rails already has a method, simple_format, that converts line breaks.

<%= simple_format(h @user.address) %>

Inject/Reduce doesn’t work on lists of ActiveRecords

Problem:

The following line of code for calculating the total order cost returns “#”.

@purchase_items.reduce() {|cost, item| cost += item.qty_ordered * item.unit_price }

Cause:

The default memo item for a reduce operation in Ruby is the first item on the list (because of this, it starts the reduce on the second item).

Solution:

You can set the initial memo item by passing it to the reduce function.

@purchase_items.reduce(0) {|cost, item| cost += item.qty_ordered * item.unit_price }