Ruby on Rails routes.rb - ruby-on-rails

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'

Related

rails ArgumentError: You should not use the `match` method in your router without specifying an HTTP method

I am getting this error message:
ArgumentError: 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"
Here is my routes.rb:
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
root 'static_pages#home'
resources :contacts, only: [:new, :create]
resources :users
resources :account_activations, only: [:edit]
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/subscribe', to: 'static_pages#subscribe'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get 'sessions/new', to: 'sessions#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy' , via: :delete
get '/resend', to: 'users#resend'
post '/resend_activation', to: 'users#resend_activation'
end
I will appreciate any help in locating what i have done wrong with my routing and how best to solve the problem.

No route matches [GET] "/edit_user/1" ruby and rails

I have write down this in routes file
resources :users
match '/signup', to: 'users#new', via: [:POST, :get, :post]
match '/edit_user', to: 'users#edit', via: 'get'
and this is my function in users controller
def edit
#user = User.find(params[:id])
end
I am getting error Please help me..
No route matches [GET] "/edit_user/1"
match '/edit_user', to: 'users#edit', via: 'get'
You are missing the /:id on the route. Fixed:
match '/edit_user/:id', to: 'users#edit', via: 'get'

Reduce complexity of routes

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

Static routes no longer working, passing id

I had routes like this
/state/arizona/unit/AZ22
I wanted to remove the controller names of STATE and UNIT and get routes like this
/arizona/AZ22
Found this code which worked great
resources :states, :except => [:index ], :path => '/' do
resources :units, :except => [:index ], :path => '/'
end
But now my static paths don't work because it thinks it is a state. I have a list of States on the static pages.
Error: Couldn't find State with id=about
Code throwing error : def set_state
#state = State.friendly.find(params[:id])
end
These are my static page routes
match '/contact', to: 'static_pages#contact', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
root 'static_pages#home'
Is there a way to fix the routes?
Or did I use static pages wrong because I'm feeding a list of states to them?
Routes are matched in the order they are specified. So, organize your routes as follows and give it a try:
root 'static_pages#home'
match '/contact', to: 'static_pages#contact', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
resources :states, :except => [:index ], :path => '/' do
resources :units, :except => [:index ], :path => '/'
end

Rails proejct, "Routing Error No route matches "/pages"" Need help

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 :)

Resources