How to route "/" to public/index.html in routes.rb? - ruby-on-rails

My Facebook app uses a AS3 front end and a APIish Rails backend.
Facebook requires your "canvas page" (your app page sucked into the Facebook chrome) either be dynamic (index.erb) or a directory (end with "/"). Since I don't know Rails views, I went with the url of "myapp.herokuapps.com/".
Heroku logs give this error:
ActionController::RoutingError (No route matches "/")
How can I match "/" to the index file?
match "/" => ????
I am used to routing resources, but not static pages.

If you want to display a static page as your root, you should be using the High Voltage gem. This allows you to add static pages to your site as follows:
Add gem 'high_voltage' to your Gemfile
Put your static home.html.erb page in /app/views/pages/
Route to your static page with root to: => 'high_voltage/pages#show', :id => 'home'
No redirects are necessary.
The High Voltage docs can be found at https://github.com/thoughtbot/high_voltage.

You need to define a root_path in your routes.rb file
So root :to => 'YOUR_CONTROLLER_NAME#YOUR_CONTROLLER_ACTION' should do it.
If you wanted to map it to a file, then just give the name of the file in the public directory, so if you wanted to map public/index.html to root then this would work:
root :to => 'index'

Both answers are correct; however, the folks at thoughtbot recommend the following:
Under "config/initializers/" create the file "high_voltage.rb"
Write in the following code
HighVoltage.configure do |config|
config.home_page = "home"
end
Restart your server
You can check the documentation at https://github.com/thoughtbot/high_voltage#specifying-a-root-path

Related

How can I set a static page (high_voltage) as root in Rails?

I used gem high_voltage to get static pages and it worked, but now I want to define one of them as root.
What should I write in Routes.rb since I don't have a controller?
You can configure the root route to a High Voltage page like this:
# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
config.home_page = 'home'
end
Which will render the page from app/views/pages/home.html.erb when the '/' route of the site is accessed.
Note: High Voltage also creates a search engine friendly 301 redirect. Any attempt to access the path '/home' will be redirected to '/'.
Source: https://github.com/thoughtbot/high_voltage#specifying-a-root-path

Understanding routing with rails

I am trying to make a stupid simple CMS for one of my clients using rails. I generated scaffolding for Page and have been successful at creating and rendering those pages, however, the routing is ugly.
Right now, if I want to view the home page, I get a url like this: example.com/pages/1
I'd like to accomplish 2 things:
How do I set the routing so that example.com automagically grabs the page named "home"
and
How do I set the routing so that example.com/page/page_name performs a
#page = Page.find_by name: 'page_name'
Q1:
How do I set the routing so that example.com automagically grabs the page named "home"
In `routes.rb:
root :to => '[controller]#[action]'
#'pages#home' for example, if your home page is in `pages_controller`
# and uses the `home` action
Q2:
How do I set the routing so that example.com/page/page_name performs a
#page = Page.find_by name: 'page_name'
match '/page/:name', :to => 'pages#some_name', :as => :some_name
this would generate the following in $rake routes:
some_name /page/:name(.:format) pages#some_name
when you link to (or redirect, or otherwise access) this page, you'd write
link_to "click this link!", some_name_path(#SomeModel.some_name)
To accomplish the first thing you need to open the file routes.rb which is in the config folder and add the following:
root to: "home#index"
I'm assuming you have a controller named home which contains a method called index and this is the one that displays the home page.
If you want to make it the same as example.com/pages then you would have to use
root to: "pages#index"
to make a general rule you need to use
root to: "controller#method"
Don't forget to remove the index.html from the public folder too.
I recommend you make the blog application presented here:
http://guides.rubyonrails.org/getting_started.html
It can help you understand more.
Here's a solution that assumes your controller is named pages_controller instead of page_controller.
Add these routes in config/routes.rb:
root to: 'pages#show', page_name: 'home'
get 'pages/:page_name', to: 'pages#show'
For the controller app/controllers/pages_controller.rb:
class PagesController < ApplicationController
def show
#page = Page.find_by(name: params[:page_name])
end
end
Note: In rails4 the find_by dynamic finders have been deprecated... so if you're app is on 4 you should look into updating them. These docs have further details.
Also, if you're just trying to get static looking urls then I would definitely go with Marian Theisen's suggestion and use friendly_id.

How can I route to a page any time no route is available?

I want to make a simple page with a back button for the odd case that a user enters a url to a page there is not route for.
For instance, the route for foo is:
resources :foos, :except => [:index]
The user enters:
mysite.com/foos
I want to display a page that says "This page doesn't exist" and a back button.
Where do I put the html.erb file and how to I account for that in routes.rb?
Thanks
At the end of your routes.rb write:
match '*path', :controller => 'some_controller', :action => 'some_action'
or
match '*path' => 'some_controller#some_action'
Source:
rails handle 404 with url redirect
In the production mode, the 404.html in the /public folder of your Rails Application will be renderd for a Routing Error instead of displaying the error message.
As per my knowledge you have two ways to do this:
If you would like to capture errors like (404,500..etc.,) use rescue_from in ActionController. Otherwise if you just want to edit the default error pages, edit the 500.html and 404.html files in {Rails.root}/public
Example: How to properly render custom 404 and 500 pages?
2.Custom Error Page - Ruby on Rails
When a client's request can not be found in the server, a 404 redirect is made. To costumize the 404 page, simply change it in public/404.html

Rails 3 - changing index

I'm using Rails 3 and have the feeling that the syntax for changing the route for the index (http://localhost:3000) is different than from the former versions.
I'd like to open the dynamic index page (index.html.erb) of the employees-controller (which can be right now opened with localhost:3000/employees) as the default page (localhost:3000). I thought it's quite easy, because in the routes it's written:
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => "welcome#index"
So that's what I actually did: I deleted public/index.html and set root :to => "employees#index".
But when I open the server and open localhost:3000, it's still opening the "welcome abroad!"-page. Pretty weird!
So I googled the problem and found answers which said, I should write this into my routes-file:
map.root :controller => 'employees', :action => 'index'
Same here - I also still get the "welcome abroad!"-page and the rails-shell says "undefined local variable or method 'map'". (I think this is the old syntax for Rails 2...?)
match "/" => "employees#index" says routing error: No route matches "/"
So what did I do wrong? How can I solve this problem?
Why you use "map" in Rails 3? Should be:
root :to => "employees#index"
I think problem is of cookies. please clear the cookies and the refresh the page.

A very basic issue with routes in ruby

I am new to ruby and while creating a sample application found out an issue that whenever I go to http://127.0.0.1:3000/people/index by default show action is executed and index is taken as a parameter. This is server log:
Started GET "/people/index" for
127.0.0.1 at 2010-12-23 18:43:01 +0500 Processing by PeopleController#show as
HTML Parameters: {"id"=>"index"}
I have this in my route file:
root :to => "people#index"
resources :people
match ':controller(/:action(/:id(.:format)))'
What is going on here and how can I fix the issue?
The route
resources :people
creates "sub"-routes
get '/people' => 'people#index'
get '/people/new' => 'people#new'
post '/people' => 'people#create'
get '/people/:id' => 'people#show'
get '/people/:id/edit' => 'people#edit'
put '/people/:id' => 'people#update'
delete '/people/:id' => 'people#destroy'
Actually, all of these sub-routes include (.:format) at the end of the recognized path.
The path /people/index would be recognized by the route /people/:id, mapping to the action #show.
The path /people would be recognized by the route /people, mapping to the action #index.
Use the URL helpers people_path and people_url for the /people route.
To get Rails to travel backward in time to before it espoused REST and to understand /people/index, do this:
resources :people do
get :index => 'people#index'
end
You might want to watch this Railscast episode.
A couple things to keep in mind when working with your routes:
rake routes dumps the map of URLs to your controllers
When providing backwards compatibility, redirect the user to the correct path
I personally have yet to upgrade my app to Rails 3, and I'll be dragging my feet until I really need to do it (just got it out the door not too long ago). In Rails 2.x you had resource routes, but if you kept the default controller/action/id route it would fall through and resolve. It appears that is no longer the case in Rails 3. Essentially your resource routes handle all URLs in that resource namespace (/people in your case).
To provide backwards compatibility, I would add a redirect route to resolve that incompatibility.
match "/people/index", :to => redirect("/people")
The main reason for that is to prevent users from saving an incorrect URL for their personal links--while allowing legacy users to still be able to get where they meant to go.
Edit: New answer, removed pointing out the typo in the question.

Resources