开发者

Railstutorial.org Ruby on Rails Routing problem

I'm following along to Michael Hartl's Screencasts from railstutorial.org. I only have a tiny bit of PHP, html, and css experience under my belt, and I'm okay with bash commands, but object oriented languages are completely new to me.

Here's the problem: I have an app called sample_app. From the command line:

 .../sample_app$ rails generate controller Pages home contact

Now, when I look at routes.rb:

 SampleApp::Application.routes.draw do

    get "pages/home"

    get "pages/contact"

And when I look at pages_controller I see:

  class PagesController < ApplicationController

      def home
      end

      def contact
      end
end

When I attempt to go to "localhost:3000/pages/home" I get the following error page:

Routing Error

No route matches "/pages/home"

I have a suspicion there is something else going on开发者_如何学Go here, because I create a static html file in the folder Public, and could not get it to load either:

Here's my static page in Public:

<html>
<h1> TACO!</h1>
    <body>
        <p> testing <p>
    </body>
</html>

And when I attempt to navigate to "localhost:3000/taco.html" I get the same type of error:

Routing Error

No route matches "/taco.html"

  • EDIT: By the way localhost:3000 by itself does load the index.html "welcome to rails blah" page just fine..

You'd be surprised how long I have been attempting to figure this out. I have a feeling this is either a super simple fix, or something is terribly wrong. One or the other...

Just so you know: Rails 3.0.3 ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux] gems 1.4.2

Any ideas? This very well might be a RTFM kind of deal, if so just tell me, and I will look harder...


Well, it looks like the tutorial you're using is a Rails 2 tutorial. In Rails 3 the routing language changed. What you want looks like this.

SampleApp::Application.routes.draw do
  match "/home" => "pages#home"
  match "/contact" => "pages#contact"
end

Then you will create "app/views/pages/home.html.erb" and "app/views/pages/contact.html.erb". Whatever you put in these two html files will be served when you go to /home or /contact. Variables are defined in the controller file app/controllers/pages.rb. If you define:

def contact
  @contact = 'John Smith'
end

Then you can access this in "app/views/pages/contact.html.erb" like so.

<h1>Contact name: <%= @contact %></h1>


I have recreated your app so far and it works fine, i would suggest you check your installation of Rails and gem files.

EDIT. I am using rails 3 and it works fine.


I figured it out. I'm a dummy. rails was still running a server in the background from my old app...

And I think I know how it happened without me knowing it too. When I started the server for the other app, I think I remember doing:

rails s &

So that I could keep using the terminal window. But that causes the problem of not being able to easily shut the server back down with Ctrl C.

So I did lsof -i :3000

which outputed:

COMMAND   PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
ruby1.8 20667 kevin    5u  IPv4 7193288      0t0  TCP *:3000 (LISTEN)

so then i killed it with:

kill -9 20667

With a little help from google of course... Sweet! Thanks for the help, knowing that it worked on someone else's end and not mine was a good start and I did learn a lot of other things by sifting through that documentation anyway...


Thanks for posting your answer, I had the same issue occur. I don't know what all of that lsof and kill is but I closed the terminal with the server running then reran the $ rails generate controller Pages home contact and then ran $ rails server and after that my localhost:3000/pages/home worked as shown on page 81.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜