1

Ruby newbie here. I'm following the Rails Tutorial and got stuck around 5.3 re: routes.

I have 5 pages(home, about, help, contact), all with similar test set, and rspec is only failing for the tests on 'home'. Since I'm using application_helper, I shouldn't need to specify in home.html.erb, right? I also took the advise of understanding rails routes: match vs root in routes.rb and added "match '/static_pages/home' => 'static_pages#home'" to routes.db.

Been stuck on these 2 errors for a while. Please help. Thanks!

Errors:

1) Static pages Home page should have the h1 'Sample App'
Failure/Error: page.should have_selector('h1', text: 'Sample App')
expected css "h1" with text "Sample App" to return something
# ./spec/requests/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

2) Static pages Home page should have the base title
Failure/Error: page.should have_selector('title',
expected css "title" with text "Ruby on Rails Tutorial Sample App" to return something
# ./spec/requests/static_pages_spec.rb:13:in `block (3 levels) in <top (required)>'

home.html.erb

<div class="center hero-unit">
<h1>Welcome to the Sample App</h1>

<h2>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</h2>

<%= link_to "Sign up now!", '#', class: "btn btn-large btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>

static_pages_spec.rb

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

  it "should have the h1 'Sample App'" do
    visit root_path
    page.should have_selector('h1', text: 'Sample App')
  end

  it "should have the base title" do
    visit root_path
    page.should have_selector('title', text: "Ruby on Rails Tutorial Sample App")
  end

  it "should not have a custom page title" do
    visit root_path
    page.should_not have_selector('title', text: '| Home')
  end
end

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag    "application", media: "all" %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>
    <%= render 'layouts/shim' %>    
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
      <%= render 'layouts/footer' %>
    </div>
  </body>
</html>

application_helper.rb

module ApplicationHelper

# Returns the full title on a per-page basis.
def full_title (page_title)
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
        base_title
    else
        "#{base_title} | #{page_title}"
    end
end

end

routes.rb

SampleApp::Application.routes.draw do
  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact' 
  match '/static_pages/home' => 'static_pages#home'

  root to: 'static_pages#home'
end

Let me know if you need anything else. Thanks!

1 Answer 1

5

Have you removed the public/index.html file and visually verified that you can get to / in your browser, and that the expected template is being rendered? Your spec looks OK otherwise.

2
  • That's it. Thank you! I guess not removing public/index makes the controller perform the test on the default root (public/index.html) instead of home?
    – Vanezia
    May 20, 2012 at 1:21
  • 1
    That's correct, capybara is simply requesting '/', just as you would yourself through the browser. Files in public/ take precedence over your rails routes, so the default index.html file will typically need to be removed when starting a new project. I think it has been discussed on the rails core mailing list that this should be made more apparent to newcomers - maybe it will make it into 4.0 :). May 20, 2012 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.