I folllowed this :
http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec:user_signup
.
But when I try to access http://localhost:3000/pages/ it returns "Routing Error No route matches "/pages""
This is my routes.rb
Sample4App::Application.routes.draw do
get "users/new"
match '/signup', :to => 'users#new'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/', :to => 'pages#home'
end
This is my home.html.erb
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
I tried everything I can. But still.
Really need help. Thanks
It seems like your missing the actual route for '/pages/'. Try adding this to your routes.rb
match '/pages' => 'pages#home'
Try this:
Sample4App::Application.routes.draw do
get "users/new"
match 'signup' => 'users#new'
match 'contact' => 'pages#contact'
match 'about' => 'pages#about'
match 'help' => 'pages#help'
match 'pages' => 'pages#home'
root :to => 'pages#index'
end
And make sure you have index action in your Pages controller.
Add this in roots
root :to => 'pages#home'
So, you can access http://localhost:3000
or add
match '/pages', :to => 'pages#home'
so you can access http://localhost:3000/pages
try this
root :to => 'pages#index'
like everyone said earlier but did You deleted index.html.erb from /public/ folder? Delete it and try again - this should resolve the problem :)
Related
I am attempting to setup my routes.rb so that /sessions/ is not required in the url for logging in and out of the site. Below are my samples to show what I am trying to achieve. Whilst the "second attempt" does in fact do what I want, I'd like to know if there is a more efficient way of doing this. I am very new to rails and I am sure that the routes.rb has some option that can do what I am doing in three large lines.
First attempt
routes.rb
namespace :account do
resources :users
resources :sessions
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
account_users GET /account/users(.:format) account/users#index
...
account_sessions GET /account/sessions(.:format) account/sessions#index
POST /account/sessions(.:format) account/sessions#create
new_account_session GET /account/sessions/new(.:format) account/sessions#new
edit_account_session GET /account/sessions/:id/edit(.:format) account/sessions#edit
account_session GET /account/sessions/:id(.:format) account/sessions#show
PATCH /account/sessions/:id(.:format) account/sessions#update
PUT /account/sessions/:id(.:format) account/sessions#update
DELETE /account/sessions/:id(.:format) account/sessions#destroy
Second attempt
routes.rb
namespace :account do
resources :users
match '/login', :controller => 'sessions', :action => 'new', :via => [:get]
match '/login', :controller => 'sessions', :action => 'create', :via => [:post]
match '/logout', :controller => 'sessions', :action => 'destroy', :via => [:delete]
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
account_users GET /account/users(.:format) account/users#index
...
account_login GET /account/login(.:format) account/sessions#new
POST /account/login(.:format) account/sessions#create
account_logout DELETE /account/logout(.:format) account/sessions#destroy
Can this be done without having to manually specific the match locations? All I want to do is remove /sessions/ as a requirement.
namespace :account do
resources :users #-> account/users
resources :sessions, path: "", path_names: { new: "login", create: "login", destroy: "logout" } #-> accounts/login, accounts/logout
end
I hope you realise you have /login twice in your second example. This simplifies it a bit but you will always have to match each route you want to specify outside any defaults.
namespace :account do
match '/login', to: 'sessions#new', via: [:get]
match '/logout', to: 'sessions#destroy', via: [:delete]
end
In rails3 we should use with_options in following way:
scope '/account' do
match '/login' => "sessions#new", :as => :login
post '/:login' => 'sessions#create', :as => :signup_create
delete '/:logout' => 'sessions#destroy', :as => :logout
end
I am creating a website were users can post jobs and users can login,register etc. I have created my jobs model and created my user model for the login/register part. When I try to load rails server I keep getting this error below and cannot figure out what I'm doing wrong or how to fix. I originally was trying to use devise and create the users model but was having issues so I deleted the files for it. I am wondering if I deleted something or if I am missing something in my routes.rb file. Can someone help or point me in the right direction? I will post my routes.rb file as well. Thanks for any guidance as I am still new to rails. The only thing I added to the routes.rb file was root :to => "sessions#login" and below that. Im sure other info in this was added when i created the models and controller.
/home/whitey7/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/mapper.rb:181:in `default_controller_and_action': missing :controller (ArgumentError)**
Routes.rb
Application.routes.draw do
get "sessions/login,"
get "sessions/home,"
get "sessions/profile,"
get "sessions/setting"
get "users/new"
resources :jobs
root :to => "jobs#index"
root :to => 'home/index'
root :to => "sessions#login"
match "signup", :to => "users#new"
match "login", :to => "sessions#login"
match "logout", :to => "sessions#logout"
match "home", :to => "sessions#home"
match "profile", :to => "sessions#profile"
match "setting", :to => "sessions#setting"
I think you still have a route in routes.rb which in invalid. Please recheck all the routes, corresponding controller and action. Please also share the full error trace, that way we can pin-point the issue.
Please check if the jobs controller is still there. Because this is the first root directive in your routes declarations (and still, it does not make sense to have more than one), Rails is checking if this root route is available. It seems that the jobs controller is missing and causing this error.
At first, check Rails routing documentation. I think you are getting this error because you are unable to define route file. The problem I figure out in your route file are :-
a. You have three different root in your route file.
root :to => "jobs#index"
root :to => 'home/index'
root :to => "sessions#login"
b. You are defining same routes multiple times.
get "sessions/login,"
get "sessions/home,"
get "sessions/profile,"
get "sessions/setting"
get "users/new"
match "signup", :to => "users#new"
match "login", :to => "sessions#login"
match "home", :to => "sessions#home"
match "profile", :to => "sessions#profile"
match "setting", :to => "sessions#setting"
The solution might be as follows :-
a. Fix the root path at first. Which path you want to make root whether it is jobs index or home index or sessions login.
b. I think you are trying to define your routes as such
match "signup", :to => "users#new", via: :get
match "login", :to => "sessions#login", via: :get
match "home", :to => "sessions#home", via: :get
match "profile", :to => "sessions#profile", via: :get
match "setting", :to => "sessions#setting", via: :get
I'm stuck in a Rails tutorial trying to configure Rails routes.
This is my routes.rb:
SampleApp::Application.routes.draw do
match '/', :to => 'static_pages#home', :via => :get
match '/help',:to => 'static_pages#help', :via => :get
match '/about', :to => 'static_pages#about', :via => :get
match '/contact', :to => 'static_pages#contact', :via => :get
When I try to access "localhost" I get:
No route matches [GET] "/static_pages/help" Try running rake routes
for more information on available routes.
I also tried:
match '/', :to => 'static_pages#home'
match '/help',:to => 'static_pages#help'
match '/about', :to => 'static_pages#about'
match '/contact', :to => 'static_pages#contact'
but that gives the same error. It only works when I use:
get 'static_pages/about'
How can I get localhost to work?
Try putting the following at the end of routes.rb
match ':action' => 'static#:action'
A request to /help will then render app/views/static/help.html.erb. Don't forget to create the static controller though, even if it's empty.
Try this:
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
I've read official guide but still have misunderstanding.
Is this code able for refactoring?
match '/help', :to => 'home#help'
match '/contact', :to => 'home#contact'
match '/about', :to => 'home#about'
help, contact and about are the only actions in the controller home.
I did this on a hunch and it's not mentioned in the documentation, but it looks like it works (I'm on rails 3.1):
controller :home do
get 'help'
get 'contact'
get 'about'
end
This also creates the help_url, help_path, etc helpers.
One warning though, this restricts the http verbs to GET. If you have a POST action (as an example, for a contact form), you could do either:
controller :home do
get 'help'
match 'contact', :via => [:get, :post]
get 'about'
end
or just:
controller :home do
get 'help'
match 'contact'
get 'about'
end
which will allow all http verbs on the contact route. But I find it better to be explicit about the accepted verbs.
You should be able to use rails shorthand here and do:
match 'home/help'
match 'home/contact'
match 'home/about'
Since the method names match this should work.
you could of course do
match '/:action', :controller => :home, :constraints => { :action => /^(help|contact|about)$/ }
but this is neither prettier, nor really shorter
i am following a rails tutorial from this link:
http://ruby.railstutorial.org/chapters/filling-in-the-layout#code:static_page_routes
in the /config/routes.rb file, i have
SampleApp::Application.routes.draw do
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
end
when i run the site, it gives me an error: no route exist pages/home. i search around the forum and ppl suggest putting match '/pages/home' => 'pages#home'
which i did:
SampleApp::Application.routes.draw do
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/pages/home' => 'pages#home'
root :to => 'pages#home'
end
everything works. but now, my question is, what is the difference between
1. match '/something', :to => 'pages#something'
2. match '/something' => 'pages#something'
3. root :to => 'pages#home'
basically, the code i just put. shouldn't the root takes take of the main home page and i wont' need match pages/home => pages#home?
so confusing,
Thanks!
EDIT1: I'm not getting the answers I want and so I assume my question is wrong. I'll break it down into 2 parts:
What is the difference between:
match '/pages/home' => 'pages#home'
AND
root :to => 'pages#home'
some say that root takes it to your root page which i can understand but as i explained above, if i just have root to: the pages/home shows a routing error. pages/home should be the same as the root page, correct?
what is the difference between:
match '/contact', :to => 'pages#contact'
AND
match '/pages/home' => 'pages#home
syntactically, the first line has the :to => and the 2nd line does not. is the to: needed? what does it do?
thanks
As far as I know
match '/something', :to => 'pages#something'
match '/something' => 'pages#something'
are equivalent. It isn't uncommon to find more than one way to say the same thing in Rails. Shorthand notation abounds for commonly used methods. If you care, the latter is what I use and see more often.
As far as the root route is concerned, here is what is going on: root :to => 'pages#home' is mapping "/" to the home method in pages_controller.rb, as you already know. But using "pages#home" does not create the url "pages/home". All it does is tell rails what to execute when it encounters "/". That is why you need to also tell rails what to do when it encounters "pages/home". Route definitions are a one-way deal.
There is a lot more I could say, but I will try to keep my answer brief. Let me know if you need more clarification. Also, this rails guide is a great resource.
root :to => 'pages#home'
The url / will be mapped to pagescontroller home action.
/something will be the url mapping for the pagescontroller's something action
root :to => "pages#home"
is the default route, i.e. when you go to "yourdomain.com/" it routes to the home action in the pages controller.