Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 857
DreadPirateShawn Avatar asked Apr 24 '12 06:04

DreadPirateShawn


People also ask

How do you write Hello World in Ruby?

Writing Code To print in Ruby, you need to use the method puts which is short for "out*put s*tring." And because Hello World! is a string, you need to surround your text with "" . puts "Hello World!"

Is Ruby on Rails still relevant 2022?

Ruby's and Ruby on Rails' Overall Popularity Although way behind main contenders, such as PHP or Python, Ruby still makes the cut for the 20 most popular programming languages list in 2022. The 2022 edition of Stack Overflow Annual Developer Survey also places RoR in a similar spot.

Is Ruby on Rails hard to learn?

Ruby on Rails is easy to learn, if you start with the right tutorial. I'm the author of the book, Learn Ruby on Rails, which I wrote specifically for beginners (it's free, by the way, so don't earn any money by recommending it). In 2021, the Ruby language is enjoying a bit of resurgence (Ruby 3.0 is now out).

What is better Ruby on Rails or Javascript?

Performance – Ruby on Rails has medium level performance as it doesn't support asynchronous programming. It also has a high CPU processing time, while Javascript has greater performance with asynchronous support and event driven single threaded architecture.


2 Answers

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!

like image 120
Brett Sanders Avatar answered Sep 21 '22 23:09

Brett Sanders


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.

like image 20
DreadPirateShawn Avatar answered Sep 21 '22 23:09

DreadPirateShawn