I have an application where they have used index.rhtml in the main folder as start up page.
So how can I do this on my own using Ruby on rails i.e., How to create rails application with rhtml page as start up file.
Please post links if any for reference.
Thanks,
Kumar Raja Donthamsetti.
Delete public.html
Add 'map.root :controller => "foo"' to routes.rb
and then of course create the controller foo with the action index.
Related
Just wanted to know, what does this line mean in the routes.rb file:
AppName::Application.routes.draw do
Please explain. I am new to Rails.
Have a read through this page.
Basically, within the block passed to Application.routes.draw (which is just a call to a method defined in ActionDispatch::Routing module within the Rails core framework), you define all the URLs/Paths that you want your Rails application to respond to.
You can see all these route definitions, by running:
rake routes
in your terminal.
It is the main routes file which defines the root and other paths for the link.
It is used as suppose you want to change your index page from default ruby on rails to your index page you make changes to file and add
root to: "controllername#index"
This file is also used to add the model to the application
resources: "model_name"
Apart from this you can also define links in your rails application
get 'courses/index'
So going from courses controller to view of the index.
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'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.")
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.