Ruby On Rails "Hello World" page doesn't work - ruby-on-rails

I'm new in ruby on rails and I have a problem. When I starе server " rails server and going to the page
www.localhost:3000
in my browser everything works fine with ROR default page, but.... after making controller rails generate controller demo index
and going to the page
www.localhost:3000/demo/index , localhost:3000/demo - the same
- there is nothing, seems like page doesn't exist, only a blank page. I tried to change index.html.erb code
<h1>Hello World</h1>
- no change. I want to practice with ROR, but can't because of that bug.
Work environment: Windows 7 64-bit
Database: MySQL

You need to have a view corresponding to your action.
If your controller is called Demo and your action is index it should be
# app/controllers/demo_controller.rb
class DemoController < ApplicationController
def index
end
end
and the view file should be at app/views/demo/index.html.erb.

The index action is the base action of the controller. Try just:
http://localhost:3000/demo
to access the index.
By default routes /demo/index is the show action with the id of index.

make sure
views/layout/application.html.erb has yield and index.html.erb have some text

check your routes.rb
root :to => 'demo#index'
Go to http://localhost:3000/

Uncomment this line from your config/routes.rb file
match ':controller(/:action(/:id))(.:format)'
Remove index.html file from public/index.html

First of all delete or rename the index.html file from public folder.
Then set root :to => 'demo#index' at route file.
Then restart the server by using this command. rails s by default your server will run at port 3000.
Now simply type http://localhost:3000/ or http://localhost:3000/demo/index

Have you deleted
app_root
--public
----index.html
yet?
Rails will look in the public folder first for a matching file before it will goto build one from your views.

As indicated by João there, you will also need a view for that. Additionally, delete public/index.html and finally add a route for your controller.
If you had created a scaffold Rails start guide you would already have the route there for you.

Related

rails generate controller pages generates but local host doesn't show new home page

I am working with "One month rails" (rails 4), used 'rails generate controller pages home' for new home page. It all seems fine but the local host page is still stating "welcome aboard
You’re riding Ruby on Rails!" and not the new home page as expected. any idea what might be the problem?
# config/routes.rb
root to: 'pages#home'
There are 3 steps that should be done:
map the new controller and action to your root_path in your config routes (as itsnikolay suggests )
remove the default index page public/index.html (as ardochhigh suggests)
restart you development server
You should change your "root" mapping realtionship in config/routes.rb. Make sure "root" point to the page (controller#action) you want.
You need to delete the default index page.
Go to your Rails root directory and delete public/index.html
rm public/index.html

No Route Matches for a very basic Rails4 App

I'm very new to RoR and I'm trying to get a very basic site going. I have my home page working okay, but when I try to add a new page, I keep getting "No route matches" in the log. Here is the output from rake routes:
Prefix Verb URI Pattern Controller#Action
inventing_index GET /inventing/index(.:format) inventing#index
ideas_index GET /ideas/index(.:format) ideas#index
root GET / ideas#index
However, when I go to mysite.com/inventing or mysite.com/inventing/index I get the no route matches error. mysite.com/ shows the app/views/ideas.erb as hoped. All I did was rails generate controller inventing index. Is there something else I have to do to activate the route?
I'm running ruby 2.0.0p247 and rails 4.0.0 with passenger/apache on centos 6. I installed all the ruby/rails/passenger stuff, so its possible something isn't setup properly.
Thanks
EDIT: Here is my routes.db file:
Rortest::Application.routes.draw do
get "inventing/index"
get "ideas/index"
root to: 'ideas#index'
end
tldr: Your problem is probably the route. Change get 'inventing/index' to get 'inventing/index'=> 'inventings#index' to correctly point the route to your controller action.
There are four things you need to do when adding a new page/route. I usually do them in this order for static pages:
1) Create a controller with the appropriate action for each page
You already did this with rails generate controller inventing index, but make sure that:
In your app/controllers folder, you do indeed have a file called inventings_controller.rb
In inventings_controller.rb, you have at least this:
class InventingsController < ApplicationController
def index
end
end
2) Create a view for each controller action
Make sure that:
In your app/views/inventings folder, you have a file called index.html.erb
Puts some simple HTML in that file, like <h1>Testing123</h1>, just so you can see if it's working.
3) Add a route for each controller action
It looks like this may be the problem. In your routes.rb file, you'll need this:
get 'inventing/index' => 'inventings#index'
Your root to works because it's actually pointing directly to your controller action, but rails isn't smart enough to guess that inventing/index should go to the index action in your inventing**s** controller.
4) As others have suggested, restart your rails app
You can do this with ctrl+c at the command line, then rails s again to start it back up.

The symbol "-" creates issue when used in page names in Ruby On Rails

I am new to Ruby on rails and working in a new test application. Consider my application url is : www.test.com
I would like to place a page by creating controller and respective views, the page should look like the below
www.test.com/articles/book-test-page
I created a controller "article" (defined the action as
def book_test_page
...
...
end
and folder under view as view/articles/book-test-page.
What routing information should i add in the routes.rb file to make this url bring up the specified url work? Any suggestion would be helpful..
Thanks,
Balan
get 'articles/book-test-page', :to => 'articles#book_test_page'

Blank page when navigating to http://localhost:3000/say/hello

I'm new to Ruby on Rails and am doing my first tutorial and am running the latest version of rails 3 and ruby 1.9.2. After creating my controller and navigating to http://localhost:3000/say/hello I'm receiving a blank page. I do see the Welcome to Rails message when I just go to http://localhost:3000. I've done some Google searches and people have similar problems but there is no clear fix. I've never really worked with MVC before so the concept of routing is fairly new to me.
Below is my controller:
class SayController < ApplicationController
def hello
end
def goodbye
end
end
My view:
<h1>Say hello to Rails!</h1>
You should delete the public/index.html file as that will mess with your routing and display by default. Have you set up your routes already, and what is the exact location and filename of the template?
You will need something like in your config/routes.rb file to correctly route that url to your template/view:
match '/say/hello' => 'say#hello'
First delete the index.html file from your public folder. Then, go to the app/views and check the views for the say controller. You should have a hello.html.erb.
The answer to your particular question was answered already by Bitterzoet, but I thought you might want some alternative learning resources.
I'm not sure which tutorial you're starting with, but I find it odd that they're not using RESTful routes. You can find out what routes you have set up at the moment by going to the console and typing "rake routes". If you would like a different tutorial, I recommend the one here:
http://www.wiki.devchix.com/index.php?title=Rails_3_Curriculum
I'd also recommend http://railsforzombies.org/ as a good first-time rails experience.
A fun general line to add to config/routes is:
match ':controller(/:action(/:id(.:format)))'
While developing, this will allow you to display the controller/action in address bar for ALL controller/action/id.format etc.
Like Bitterzote wrote, if controller is "say" and action is "hello", http://localhost:3000/say/hello .
If you use controller "say" and action "move", http://localhost:3000/say/move .
I've found this route to be very useful during development, but change this if you launch your application! (Rails warns: "Note: This route will make all actions in every controller accessible via GET requests.")

Rails apache mongrel mapping controller problems

I am working with Rails for the first time and got the Apache-Mongrel integration working and have the index.html page showing up when I open localhost in the browser.
I created the controller (ruby script/generate controller Welcome index) and renamed the index.html file.
However, when I try to map to a controller in routes.rb (map.root :controller => "welcome") things break. I can start the ruby server (ruby script/server) and browse to localhost:3000 works.
You are making a reference to a controller named "Welcome". Renaming the Html file in the public folder is useless. You should create a controller called "Welcome " and then add a method, such as "index".
And then in the routes.rb file you should add: "map.root: controller => 'welcome',: action => 'index'"
Delete index.html file too, it will prevent your routes from working.

Resources