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
Related
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'
Im using RoR to write my website and i've run into this issue with routes.
My routes file looks as such
root to: "static#index"
match '/index', to: 'static#index', via: [:get, :post]
match '/download', to: 'static#download', via: [:get, :post]
match '/terms', to: 'static#terms', via: 'get'
match '/privacy', to: 'static#privacy', via: 'get'
match '/jobs', to: 'static#jobs', via: 'get'
match '/android', to: 'static#android', via: [:get, :post]
match '/apply', to: 'static#apply', via: 'get'
match '/faq', to: 'static#faq', via: 'get'
When I preform a GET request to https://www.website.com/ the page gets displayed.
However when I preform a POST request to the same address it returns a 404 page not found.
I do not have a index.html in my public directory.
So why is this POST request returning a 404 when it is matched to both GET and POST and the GET works correctly?
EDIT
i've added this line
match '/', to: 'static#index', via: [:get, :post]
just below my root to declaration, however now my page shows a 422 error instead of a 404
You can try this:
post '/' => "static#index", as: "root"
Rails routes: resourcing from the root path "/"
Your root URL is the one that handles https://www.website.com/ from your example. When you specify
root to: "static#index"
You have declared a GET route for /, which maps to StaticController#index. The POST request that you are making to https://www.website.com/ is not handled by the root route, thus properly returns a 404.
I'm guessing that you want the POST request to go to either /index, /download or /android. You need to specify the endpoint fully in the URL in order for the request to be routed properly.
My preference and I think the cleanest implementation would be:
resources :static, :only => [:index, :create], :path => "/"
I'm using Rubymine (5.4.3.2.1) for Hartl's RoR tutorial and I'm having some troubles with path helpers. root_path works just fine but rubymine says 'cannot find xxxx_path' for the rest actions in my controller.
Rspec and Rails server are working just fine with those same path helpers!!
My routes.rb:
SampleProject::Application.routes.draw do
get "users/new"
root 'static_pages#home'
match '/signup', to: 'users#new', 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'
end
I also tried to use get instead of match but that didn't help.
Ruby is a dynamic language and therefore some things are hard to parse intelligently for the IDE.
However showing routes and helpers like "people_path" etc should work, but you must restart the server if you change it!
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.
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'