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
Related
I am a rails newbie (building my first app) and right now my routes.rb is quite a mess. I was wondering what is the best way to organize/format all the content so it is easy to see what is going on and avoid silly routing errors.
Any general tips or simplified examples would be appreciated.
Routes.rb
Rails.application.routes.draw do
resources :posts
get 'users/index'
#devise_for :admins
namespace :super_admin do #superadmin stuff
resources :dashboard, only: [:index]
end
devise_for :super_admins, path: "super_admin", controllers: { registrations: "registrations", sessions: "super_admin/sessions" } #lets super admin sign in
get 'welcome/index'
root to: "welcome#index"
match '/teachers', to: 'teachers#index', via: 'get'
#route to delete users
match 'users/:id' => 'users#destroy', :via => :delete, :as => :admin_destroy_user
match '/users/:id', to: 'users#show', via: 'get'
#routes for registration
devise_for :users, controllers: { registrations: "registrations" }
devise_for :teachers, controllers: { registrations: "teacher/registrations" }
get 'users/:id/posts' => 'users#posts', :as => :user_posts
match '/users', to: 'users#index', via: 'get'
match '/about', to: 'about#index', via: 'get'
match '/teachers/:id', to: 'teachers#show', via: 'get'
match '/teachers/list', to: 'teachers#list', via: 'get'
get 'super_admin/dashboard/new_user', :as => :super_admin_new_user
resources :users, :only =>[:show]
Unfortunately it's simply part of rails that this file gets messy over time. Our app has hundreds of entries for various items that have been added over the years, so I know from experience it's good to think of from the beginning.
The number one thing you can do to keep the file organized is to add lots of comments, with some kind of consistency that helps you understand how they match your app, for example:
# ADMIN FUNCTIONALITY
# -- Allows super admin access and functionality
# your admin stuff here
And then keep your routes for certain functionality in the same section. In your example, you have a "teachers" route near the top, and then some more near the bottom. Keep those grouped together and commented and it'll be easier to manage in the long run.
I wish to map new_user path of the user resource to 'ROOT_DOMAIN/new' URL. My routes.rb looks something like this:
#new_user GET /users/new(.:format) users#new
# User Resource
match 'new', :to => 'users#new', via: [:get, :post]
resources :users do
collection do
match 'new', :to => 'users#new', via: [:get, :post]
end
end
So while I'm able to hit the /new URL to the desired action with
match 'new', :to => 'users#new', via: [:get, :post]
but the new_user path still leads to '/users/new'. How to remove the controller_name from the new_user method?
Your new_user_path takes you to user#new because that's how rails build resourceful routes. Have a look at rails guides to learn more. You need to pass the as: option to your routes in order to give them proper helper methods like:
post '/home' => "home#index", as: :home
This will give you two helpers home_path and home_url
Now you have
resources :users do
collection do
match 'new', :to => 'users#new', via: [:get, :post]
end
end
If you do rake routes in your terminal you'll see that new_user helpers are assigned to user#new. You need to give it a different path helper and then assign your new_user helpers to your custom route
resources :users do
collection do
match 'new', :to => 'users#new', via: [:get, :post], as: :old_new_user
end
end
and then you can use your new_user helper for your custom route
match 'new', :to => 'users#new', via: [:get, :post], as: :new_user
You can check your path helper by doing rake routes in terminal
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'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 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 :)