Rails 3 in Action Errata

The 1st edition for Rails 3 in Action contained some errata. Listed below is the known errata for this edition and the fixes for them. If you do discover any more, please do not hesitate to let us know on the Rails 3 in Action Forum.

It is our priority to fix these up and deliver you the best book possible.

Chapter 1

Rails requires a JavaScript runtime (PT: 20333079)

Ruby on Rails 3.1 requires a JavaScript runtime for default gems used such as uglifier. If you do not have one installed, you will see this error:

Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

Please consult the README for execjs, which mentions possible runtimes. We would recommend the Node.js, as it provides the V8 JavaScript engine and is most likely installable using the package manager for your system if you're on a Linux-based system.

Page 8: Listing 1.1 - Migration example is incorrect (PT: 21819899)

The mgiration inside this listing still uses the old Rails 3 syntax and may confuse readers who are unfamiliar with it. The migration command will actually output a migration that looks like this:

  class CreatePurchases < ActiveRecord::Migration
    def change
      create_table :purchases do |t|
        t.string :name
        t.float :cost
        t.timestamps
      end
    end
  end

The self.up and self.down methods can still be used, but are now primarily used when it's not clear to Rails how to run the migration both forwards and backwards. In this migration's case, it is obvious to it (running it forward creates the table, running it backwards drops the table) and so we can use the change syntax.

Page 10: after Listing 1.2 (PT: 19450643)

The following sentence is in error:

By using <%=, the Ruby code is evaluated but not output.

It should read like this:

By using <%=, the Ruby code is evaluated and the result is output.

Page 51: cucumber-rails version should be specified.(PT: 21821827)

The latest version of Cucumber does not contain the web_steps.rb file. The book was written for cucumber-rails 1.0.6 and this change came shortly after the book was published.

To fix this, specify a version for the gem in the Gemfile:

      gem 'cucumber-rails', '1.0.6'
    

Page 81: dynamic_form gem location is incorrect (PT: 18476389)

It is suggested to put the dynamic_form gem underneath the coffee-rails gem inside the Gemfile. This is in error, as this would place it inside the :assets group meaning that it would not be loaded in the development environment.

Rather than placing this gem inside the assets group, this gem should be placed immediately after it to ensure it is always loaded in all environments.

Page 94: rake assets:precompile should be run before launching the production environment (PT: 18476389)

Due to asset serving having been turned off automatically in the production environment, assets will need to be precompiled by running rake assets:precompile before launching the server in production. If this is not run, the styles for the application will be missing.

After this example we should use rake assets:clean to clean the assets out of the public/assets directory, otherwise they would be cached and any modifications we made to assets inside app/assets would not come into play.

Page 196: Listing 8.12 and Listing 8.13 (PT: 21821707)

The links for "Edit" and "Delete" inside Listings 8.12 and 8.13 should instead be for "Edit Ticket" and "Delete Ticket" respectively.

Page 206: table class is missing for permissions table (PT: 19679359)

The table element in Listing 8.16 requires a class attribute to be styled correctly.

This beginning of this element should be defined like this:

<table class='permissions'>

Pages 228 & 229: assets_controller_spec.rb -> files_controller_spec.rb (PT: 19679847)

The references to spec/controllers/assets_controller_spec.rb on these two pages should actually be referring to spec/controllers/files_controller_spec.rb instead.

Page 236: The new action in the FilesController is overdone (PT: 19758699)

The code in this action is excessive and results in this:

  def new
    @ticket = Ticket.new
    asset = @ticket.assets.build
    render :partial => "files/form",
     :locals => { :asset => asset }
  end

This could be simplified into just this:

  def new
    @ticket = Ticket.new
    @ticket.assets.build
    render "files/form", :asset => asset
  end

Page 266: Listing 10.20 (PT: 20037281)

The code inside this listing that reads as this:

  "→" + render(comment.state)

Should instead read like this:

  render(comment.state)

Pages 274 & 275: State li element regression (PT: 20037269)

On page 274, the li element for a state looks like this:

  <li id='state_>%= state.id %>'>
    <%= state.name %>
    <% if state.default? %>
      (Default)
    <% else %>
      <%= link_to "Make Default", make_default_admin_state_path(state) %>
    <% end %>
  </li>

Later on, on page 275, it is recommended to change this element to this:

  <li id='state_>%= state.id %>'>
  <%= state.name %>
  <%= link_to "Make default", make_default_admin_state_path(state) %>

This is in error. This element on page 274 should just update the li's element to have an id attribute, like this:

  <li id='state_<%= state.id %>'>

While the element on page 275 should be the one to add the link:

  <li id='state_>%= state.id %>'>
    <%= state.name %>
    <% if state.default? %>
      (Default)
    <% else %>
      <%= link_to "Make Default", make_default_admin_state_path(state) %>
    <% end %>
  </li>

Page 282: Section 10.5.3: Hacking a form (PT: 20337261)

The first code example on this page suggests using the Factory method within the console to generate a new object:

  user = Factory(:user)
  user.confirm!
  user.permissions.create(:object => Project.first,
                                 :action => "view")

This won't work in the development environment as it has not loaded the factory definitions. To fix this, FactoryGirl.find_definitions must be called before referencing the factory:

  FactoryGirl.find_definitions
  user = Factory(:user)
  user.confirm!
  user.permissions.create(:object => Project.first,
                                 :action => "view")

Page 292: Missing permission step (PT: 20037153)

The Scenario in Listing 11.2 is missing a step for giving the user permission to change the state. The first line of this Scenario should be this:

  Given "user@ticketee.com" can change states on the "Ticketee" project

Without this line, the user does not have permission to change the states and so they are unable to do so.

Page 323: Content type checking is too strict (PT: 21821377)

The content type checking in the untitled listing here is too strict. It performs this:

  p.content_type == content_type
    

Where instead it would be better to check for inclusivity:

  p.content_type.include?(content_type)
    

Chapter 13: include_root_in_json (PT: 19208829)

The objects returned from the API in this chapter may not contain the element's type as the root node due to a last-minute change in Rails 3.1. This issue is adequately shown in this Stack Overflow post.

If you are encountering this issue, it is recommended that you create a new file called config/initializers/wrap_parameters.rb and define this content in it:

  ActiveRecord::Base.include_root_in_json = true

Page 352: Incorrect directory name "spec/v1/api"(PT: 22124047)

The directory "spec/v1/api" is incorrectly, and should instead be "spec/apis/v1". This is used to reference the projects_spec.rb file in the second paragraph on this page.

Page 363: Listing 13.12 (PT: 21820573)

This Listing contains two if statements checking for @current_user.admin?. There should only be one.

Page 366: Listing 13.13: context block (PT: 21820541)

The description leading into Listing 13.13 says that we use a context block, but we actually don't.

Page 373: Listing 13.11 (PT: 21820561)

This Listing incorrectly references the method to get the status code for the response as code in this line:

  last_response.code.should eql(401)
    

This line should actually read like this:

  last_response.status.should eql(401)
    

Page 374: Section 13.2: 5 tickets, not 20 PT: 21821193

This page mentions that we need to create 20 tickets, but the example creates 5:

With this setup, you can begin the context for your index action and then in a before block, create 20 tickets for the project by using these lines after the let(:project) line:

This text should read "create 5 tickets", not 20.

Page 498: @@user_class is incorrectPT: 22124133

The reference to the @@user_class variable on this page is incorrect. A variable with the double-@ prefix (@@) is a class variable, where in this case we should be referencing an instance variable for the class.

Page 498: Listing 17.12: Need to reset user_classPT: 22124137

The config/initializers/forem.rb initializer pre-initializes the user_class variable and so the test in Listing 17.12 will always break. This variable must be reset to nil before this test and reset after it, by putting this code at the beginning of it:

  before do
    Forem.user_class = nil
  end

  after do
    Forem.user_class = User
  end

Page 499: Definition for self.user_class is wrongPT: 22124163

This method incorrectly references the @user_class variable as just @user and the ConfigurationNotSet exception as ConfigurationNotFound. It should be written like this:

  def self.user_class
    error = "Please define Forem::Engine.user_class" +
            " in config/initializers/forem.rb"

    @user || raise(ConfigurationNotFound, error)
  end
    

Page 515: Chapter 17 Summary: Incorrect mention of Forem::UserExtension (PT: 18611295)

In the summary of Chapter 17, there is mention of a Forem::UserExtension module. This existed in draft versions of the chapter but does not exist in this first version. It is mentioned in error.

Back Cover

On the Back Cover, in Ryan Bigg's bio, the word "Sydney" is mispelled as "Syndey".