I am stuck and unable to proceed on my Ruby on Rails tutorial.
I am doing a Ruby on Rails tutorial using a mac.
I have created a new Ruby on Rails project in Users/username/sites/simple_cms
I am using Webrick server
I started the server using rails s command and as expected, when navigating to the localhost:3000 the public index page was displayed.
I then added a controller and view using the following command in the route directory:
rails generate controller demo index
The controller and view were successfully created:
My routes file:
SimpleCms::Application.routes.draw do
get "demo/index"
My demo_controller File:
class DemoController < ApplicationController
def index
end
end
My View file is now located in views/demo/index.html.erb:
<h1>Demo#index</h1>
<p>hello world</p>
I then started Webrick again and entered:
http://localhost:3000/demo/index
but a blank page is displayed.
add root to 'demo#index' to routes file http://guides.rubyonrails.org/routing.html#using-root
Try to change your route as follows
get "index" => "demo#index"
then you can access the site with localhost:3000/index
But if this is your root path you can also do
root "demo#index"
to access the site with localhost:3000
In this case demo refers to your controller and index to your action in the controller.
Related
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.
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.
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'
I installed Instant Rails on windows and tested using http://127.0.0.0:3000 on browser it showing WELCOME page.
Next : i started a sample application using commands
> rails hello
> ruby script/generate controller Hello index
with this it created a hello app
but i am when accessing http://127.0.0.0:3000/hello
it is giving me
Routing Error
No route matches "/hello" with {:method=>:get}
You should have a view called hello.html.erb in the views/Hello folder
You have to set config/routes.rb up:
map.resources :hello
http://guides.rubyonrails.org/routing.html
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.