DreadPirateShawn
DreadPirateShawn

Reputation: 8412

Ruby on Rails: Hello World

Long-time Java dev, first-time Ruby dev. Trying to reach the "hello world" step with a Rails app, and having difficulty.

I'm positive that I'm missing something basic here. That being said, none of StackOverflow's "Questions with similar titles" nor Google's "ruby rails hello world" hits (or variants thereof) have clarified what I'm missing.

I've got ruby (v1.9.3p194), gem (1.8.23) and rails (3.2.3) installed via RVM. I generated a controller using:

rails generate controller common

In the default "config/routes.rb", I have the following two routing attempts:

Web::Application.routes.draw do
  root :to => "common#index"
  match ':controller(/:action(/:id))(.:format)'
end

When I initially ran "rails server" and loaded "http://localhost:3000/common" in my browser, I saw the following:

Unknown action
The action 'index' could not be found for CommonController

I learned this is because "index" was not defined for my common controller, so I've edited "app/controllers/common_controller.rb" to contain the following:

class CommonController < ApplicationController

  def index
  end

end

I now see the following at "http://localhost:3000/common" in my browser:

Template is missing
Missing template common/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/coding/workspace/[My] Toolbag/web/app/views"

I see the same thing at root, which makes sense.

From what I've found, this is clearly due to not having anything rendering in my controller, so I believe it should instead be checking for something at "app/views/common/index.html.erb" -- however, I do have a file there, containing the following:

<h1>Hello World</h1>

I've also tried renaming "index.html.erb" to "_index.html.erb", "index.html", and "index", all based on Googling variants of the console error "ActionView::Missing Template" and the similar browser output above.

Many of the Google results contain snippets of suggestions, but without clear guidance on which file(s) to edit with said suggestions, or else with repeats of the steps that I've already taken above.

If anyone could advise what I'm missing, I'd appreciate it.

PS: I'm running as super-user, with "root:root" ownership of all files referenced here. Might rails be intended to be run as a regular user? Doesn't seem like that would let me get quite this far if it was the case, though.

Upvotes: 13

Views: 12503

Answers (2)

DreadPirateShawn
DreadPirateShawn

Reputation: 8412

Wow. I actually just stumbled upon the actual root problem:

I was creating my project within a folder that had a bracket in the name -- that is, I was running "rails new web" from within "/coding/workspace/[My] Toolbag".

Tonight I happened to try once more in "/coding/workspace", and it finally worked. So then I tested "rails new web" from within the following locations to flesh out my suspicion:

/coding/workspace/test1
/coding/workspace/test 2
/coding/workspace/test [3]
/coding/workspace/test [4
/coding/workspace/test ]5
/coding/workspace/test [6/subfolder

Suspicion confirmed. Rails projects within folders 3,4,6 demonstrate the problem I described in this question, while rails projects within folders 1,2,5 work just fine.

Specifically, the problem is an open bracket anywhere in the path: "["

I'm using Linux Mint. The problem might apply even more broadly, though I haven't yet attempted to repro on other distros, Windows, or Mac.

With many thanks to Brett Sanders for all of his repeated assistance and confirmation that I my approach should have been correct, I'm submitting this answer and marking this one as correct, in case anyone else encounters the same issue.

Upvotes: 10

Brett Sanders
Brett Sanders

Reputation: 813

That's strange. I just repeated your steps exactly and got it to work (using rails 3.2.3 and ruby 1.9.3-p0) and when visiting localhost:3000/common I got the "Hello World."

http://grab.by/dg6Y

Here's what I'd try real quick, to make sure it's not a problem with your setup:

1) cd inside your rails project directory

2) issue these terminal commands

rails g scaffold user name:string email:string

rake db:migrate

3) visit this url http://localhost:3000/users

If your rails is setup correctly, you should see this

http://grab.by/dg74

If these steps don't work, then something weird seems to be going on with your setup.

Also, be sure to remove the public/index.html file from your app. If you don't remove it, you won't be able to see your custom root page, which you specified in your routes file.

If above doesn't work, maybe a problem with your Ruby version?

Hope this helps!

Upvotes: 10

Related Questions