map.root does not work in ruby on rails - ruby-on-rails-3

I have deleted public/index.html.
Here is my config/routes.rb
Those all I have in that file
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.root :controller => "home"
I've also tried using map.root :controller => "home#index"
when I ran rake to check
$ rake routes
(in /var/www/atlantix)
/:controller/:action/:id
/:controller/:action/:id.:format
root / {:controller=>"home", :action=>"index"}
I also have:
app/views/home/index.html.erb
app/controllers/home_controller.rb
My issue:
I am a complete newbie of ROR.
I am using Apache and Ruby 1.8.
When I navigate to the http://localhost/myapp/,
I do not see the new home page I created.
Am I missnig something critical in my setup and configuration.
Please advise & help

Mmmm you seem to mix Rails 2 syntax. In Rails 3 you should write:
root :to => 'home#index'
Note: you should also delete the index.html from the public folder.
More information concerning routing can be found here.

Related

Rails 3.1.10 - Homepage loads twice but if I add a trailing '/' to the URL it works fine (loads once)

Server: Rails 3.1.10 and Ruby 1.9.2 on Ubuntu 11.10 (Oneiric). Client: Windows 7, Chrome 29.0.1547.66 m.
I have a small but annoying problem: checking my logs, I see that my homepage loads twice. Strangely though, it works correctly (loads only once) if I append a '/' to the URL.
This appeared after I removed some unneeded gems from bundler. I also removed the require statement for 'rails/all' in my config file and instead am using selected railties because I don't need a DB/model.
I've checked the answer here but no luck - there doesn't seem to be a missing asset source or href, and the double load also appears in 'incognito' browser mode. I also tested after disabling all extensions on Chrome.
application.rb:
#require 'rails/all' (commented out - active_record & test_unit are not needed)
#------------------------------------------
%w(
action_controller
action_mailer
sprockets
).each do |framework|
begin
require "#{framework}/railtie"
rescue LoadError
end
end
routes.rb:
match '/' => 'pages#home'
match 'root' => 'pages#home'
match '/home' => 'pages#home'
I've also tried:
root => 'pages#home'
root :to => 'pages#home'
home method in pages_controller.rb:
def home
render 'home'
end
Thank you.
Isn't the problem that you are defining the root and /? Defining / shouldn't be needed.
Normally I just define: root :to => 'pages#home'

Rails upgrade to 3.2 routes/locale fails

I have recently upgraded a project to Rails 3.2 from 3.1 and have been stuck trying to figure out why the routing is not playing nicely with devise.
NoMethodError - undefined method `locale' for #<ActionDispatch::Request:xxxxx>:
This is on the new (1)Journey routing engine. The issue for now is with Devise only other modules are not erroring. I can tell the issue is within my use of scope. It works if I get ready of the scope.
scope "(:locale)", :locale => /#{I18n.available_locales.join("|")}/ do
devise_for :users, :controllers => { :registrations => "users/registrations" }
...
end
(1) Line 7 (133). Please see trace and Journey line error in context here.
Any advice and guidance is appreciated. Thank you.
Rails 3.2, Ruby 1.9.3
Try to put :locale in a :constraints hash:
scope "(:locale)", :constraints => {:locale => /#{I18n.available_locales.join("|")}/}
I think it will help for more problems too: http://guides.rubyonrails.org/routing.html

Getting routes.rb root to work in Rails

Started working with RoR again! I'm having trouble getting my config/routes.rb file to perform. I'm getting "uninitialized constant ApplicationsController" using RubyMine.
Here's what I have changed in my routes.rb after trying to search things down:
resources :applications
root :to => 'applications#index'
application_controller.rb has:
class ApplicationController < ActionController::Base
protect_from_forgery
end
I've ran my rake routes:
applications GET /applications(.:format) applications#index
POST /applications(.:format) applications#create
new_application GET /applications/new(.:format) applications#new
edit_application GET /applications/:id/edit(.:format) applications#edit
application GET /applications/:id(.:format) applications#show
PUT /applications/:id(.:format) applications#update
DELETE /applications/:id(.:format) applications#destroy
root / applications#index
rake rails:update and all changes were approved except altering routes.rb
Heres what its kicks out Rubymine side:
Started GET "/" for 127.0.0.1 at 2012-11-11 02:50:27 -0800
Connecting to database specified by database.yml
ActionController::RoutingError (uninitialized constant ApplicationsController):
Thanks for the help!
You should rename application_controller.rb to applications_controller.rb
Check if you have a file called applications_controller in your app/controller folder, or its name is application_controller, so rename it to applications_controller, if you don't have that file, then create your ApplicationsController:
rails generate controller applications

Omniauth can't find route

I'm trying to implement Omniauth with my Rails 3 app. I followed the tutorial from the Railscast episod 205, but can't get it to work. When I call the '/auth/twitter' (it doesn't work with any provider) Rails complains it can't find the appropriate route (it tries to load my default route).
I added omniauth.rb under config/initializers/, put gem 'omniauth' in my Gemfile, and ran bundle install.
I'm not sure how to debug this problem nor what information to provide to help understand the problem.
After some research I found a solution. I added the following to my routes.rb file:
get 'auth/:provider' => 'authentications#passthru'
and to my authentications controller:
def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Not sure why I had to do that, but it's working...
Here were my steps to get it working:
1) Add omniauth to your Gemfile
2) Add omniauth strategy to your Gemfile (omniauth-linkedin for example)
3) Run bundle install
4) Add the omniauth initializer in config/initializers/omniauth.rb
5) Add the match auth/:provider/callback => users#omniauth route to routes.rb (point it to the controller/action where you'll handle the authentication response)
6) Build the action to handle the authentication response in the controller you referenced in step #4
7) Restart your web server
The default /auth/:provider route wasn't recognized for me until I restarted Apache.
You have to add 'omniauth-twitter' to your gemfile, and
:strategy_class => OmniAuth::Strategies::Twitter
to your omniauth.rb, at the end of the twitter provider.
Then it will recognize the /auth/twitter path.

can't make a page root

I am a noob in ruby on rails so excuse this question if it is a stupid one :
I've added in my routes.rb the command :
root :to => "pages#home"
a have generated a pages-controller using
rails generate controller pages
in my pages_controller.rb i've defined a function named home :
def home
#text = "da"
end
in the views folder from my app in the newly generated pages folder i've created a file named home.html.erb . in it i've placed the following command :
<%= #text %>
The problem is that when i start the server my app isn't rooted in home and even when i run
localhost:3000/pages/home it still doesn't work :
No route matches "/home"
You have created a controller but no view, try these commands
$ rails new myapp
$ cd myapp
$ rails g controller pages index <- this will create a controller **plus** an index view
$ rm public/index.html
now edit routes.rb and add root :to => "pages#index
$ rails s
open http://localhost:3000 and you'll see the new page.
I'm a noob too (I started coding in ruby 2 weeks ago)... I strongly suggest you to get a book, such as Agile Web Development with Rails 4th ed. I covered the book in a week and it gave me a LOT of insight about ruby and rails.