I have a "list" model and "ListsController" controller for it. By default, the route for lists was /lists/1, /lists/1/edit/, etc. I changed my routes.rb file to make it so the show path was "/:id", the new path was "/new".
Here's my routes file:
ToDo::Application.routes.draw do
root to: 'pages#home'
match '/about', to: 'pages#about'
match '/contact', to: 'pages#contact'
match '/help', to: 'pages#help'
resources :lists
match '/new', to: 'lists#new'
match '/:id', to: 'lists#show'
match '/:id/new', to: 'lists#new_item'
end
I can access a list by doing "localhost:3000/1" perfectly fine. But now I'm trying to use link_to, and when I do "link_to "List", list", it generates a url to the original route, which is "localhost:3000/lists/1".
Does anyone know how to fix this? Is there anything I should be doing better with my routes?
Thanks!
Instead of using match you could simply provide alternative path for resources:
resources :lists, path: ''
You will need to specify as: 'name' option to create a named route for your match rules, and to overwrite the named route provided by resource :lists.
resource :lists
match '/new', to: 'lists#new', as: 'new_list'
match '/:id', to: 'lists#show', as: 'list'
Related
How can I remove slug name from URL? I have URL with parent page:
host_url/home/index
but I need this to be as
host_url/index #without parent slug name
You can simply change the routes as:
match '/index', :to => 'home#index', via: [:get]
If using resources in routes .
resources :users, path: '/'
get '/index' => 'home#index'
routes.rb file is
Rails.application.routes.draw do
root 'pages#home'
match '/contact', to: 'pages#contact', via: 'get'
match '/home', to: 'contact#pages', via: 'get'
pages_controller is
class PagesController < ApplicationController
def home
end
def contact
end
end
I get routing error saying "uninitialized constant ContactController". Does anyone know how to fix this?
Do Change your routes.rb file
Rails.application.routes.draw do
root 'pages#home'
match '/contact', to: 'pages#contact', via: :get
match '/home', to: 'pages#home', via: :get
end
Would work!!!
In Rails 4 with match methods must specify HTTP method otherwise would get RuntimeError "You should not use the match method in your router without specifying an HTTP method. (RuntimeError)"
match is deprecated. Try
root 'pages#home'
get '/contact', to: 'pages#contact'
get '/home', to: 'pages#home'
I am just getting started with RoR and have a basic question.
Currently I am creating simple static_pages e.g. about contact etc, for my application that will route as follow:
root "static_pages#home"
match "/about", to: 'static_pages#about', via: 'get'
match "/contact", to: 'static_pages#contact', via: 'get'
Which will look as follows:
localhost:3000/about
localhost:3000/contact
Now I would to create a sub directory called "legal" with an index page for the directory and other some pages, which will look like:
localhost:3000/legal
localhost3000:/legal/terms
Would I need to create a new controller to do this and include all the action or is there a way I can do this with my "StaticPages" controller some how?
Thanks in advance :)
You can do something like:
scope '/legal' do
match "/about", to: 'static_pages#about', via: 'get'
match "/contact", to: 'static_pages#contact', via: 'get'
end
For more info check out the guides for namespacing: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
I generated a controller and changed the routes but opening the links yields errors on my local server.
Generating controller and routes
rails generate controller StaticPages home about team contact
Change routes.rb
MyApp::Application.routes.draw do
root to: 'static_pages#home'
match '/about', to: 'static_pages#about'
match '/team', to: 'static_pages#team'
match '/contact', to: 'static_pages#contact'
end
The root path work but none of the 'about, 'team', or 'contact' links work. This is the error I get:
"You should not use the match method in your router without specifying an HTTP method. If you want to expose your action to both GET and POST, add via: [:get, :post] option. If you want to expose your action to GET, use get in the router: Instead of: match "controller#action" Do: get "controller#action""
Why can't I use 'match'?
match method has been deprecated.
Use get for GET and post for POST.
get '/about', to: 'static_pages#about'
You can use match, you've gotta add a via: option:
match '/about', to: 'static_pages#about', via: :get
match '/team', to: 'static_pages#team', via: :get
match '/contact', to: 'static_pages#contact', via: :get
You can also pass other HTTP verbs to via: if you need to, like via: [:get, :post]
Source: Rails Routing Guide
First, you must specify the HTTP method by adding via: :get at the end of match 'st' => 'controller#action
And it's better to use get '/home', to: 'static_pages#home'
But, there is a problem, that your code doesn't follow RESTful, that only support 7 actions: index, new, edit, create, update, show and destroy.
These are 2 solutions:
SOL 1: Put them in different controller (homes, abouts..) and all of these controllers have action index.
SOL 2: If it's too much work, we can match them to show action. We use static_pages controller, and each page (home, about) will be a item.
The routes will look likes
/static_pages/home
/static_pages/about
I know it isn't good because of the prefix static_pages.
We can easily get rid of this by adding a custom routes at the end of routes file:
get '/:id', to: 'static_pages#show'
That's it. And if you think it's too much work (I think so too), check out this gem High Voltage. Have fun.
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