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
Related
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
Around listing 5.29 of the Hartl rails tutorial (http://ruby.railstutorial.org/) there is a discussion of a routing error that forces you to call the get method on users/new and not just a match method. This is later rectified by calling to the resources method in the next chapter.
My general question is why don't we have to call get() on the PagesController actions in the following listing.
#5.29
SampleApp::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'
root :to => 'pages#home'
end
The difference is in the types of HTTP requests the routes will match.
By using get "users/new" the route will only match HTTP GET requests.
match "users/new" would actually match all types of HTTP requests.
You can use get instead of match for your other routes if you only expect GET requests for them (which appears to be the case).
Recently I've tried to update my routes to be more specific in the routing.rb file:
resources :users
match '/signup', :to => 'users#new'
I removed the resources :users and changed the above to
match 'users/new' => 'users#new'
match 'users/show/:id' => 'users#show', :as => :users_show
match 'users/edit' => 'users#edit'
match '/signup', :to => 'users#new'
But when I try to click on the same link that I've had before in my layout page using the signup_path (http://localhost:3000/signup), I get the following error:
undefined method `users_path' but I am not using users_path anywhere
Shouldn't it still work? I haven't changed anything else in the other pages. The controller and actions are still the same.
Thanks!
What it tells is that you don have helper method named "users_path", which is created by resources in routes.rb.
Check by yourself with rake routes, what you get with your example:
users_new /users/new.(:format) {:controller=>"users", :action=>"new"}
users_show /users/show/:id(.:format) {:controller=>"users", :action=>"show"}
users_edit /users/edit(.:format) {:controller=>"users", :action=>"edit"}
(and no, you don't need forward slash at the beginning of path).
You defined only routes for some pages (show/new/edit), but you don't have path for index/create/update/destroy actions. Add resource back to routes.rb and check which routes it generates. Remember that by default your match-ed routes match all requests, not specific HTTP methods (like with resource). If you want to fully reassemble this behavior, take a look at documentation (at http://api.rubyonrails.org search for "match") and :via parameter.
Also why do you want to remove resource, and add all paths by yourself? If you want to limit available routes, then there are :only/:except options, you can also pass block and add routes by yourself, just check documentation (at http://api.rubyonrails.org and search for "resources" - it's shame I no longer can get link to specific documentation page and from Rails documentation).
I think basicxman is on the right track. You need a leading '/' in all your routes
Change:
match 'users/new' => 'users#new'
match 'users/show/:id' => 'users#show', :as => :users_show
match 'users/edit' => 'users#edit'
To:
match '/users/new' => 'users#new'
match '/users/show/:id' => 'users#show', :as => :users_show
match '/users/edit' => 'users#edit'
Also, do you still have the resources :users line at the top of your routes.rb? That'll also be necessary.
You have to add a route to create action as well. Probably your signup form points to users_path with POST method which is basically the create action. So add to your routes the following
match 'users' => 'users#create', :via => :post, :as=>:users
For more customization info please see the Rails routing guide
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 :)
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.