Rewrite the routes skip the controller name - ruby-on-rails

I want to access the following path without the controller name
e.g. SITE/about SITE/faq
How to do it ?
routes.rb
get 'welcome/index'
get 'welcome/about'
get 'welcome/brand'
get 'welcome/product'
get 'welcome/news'
get 'welcome/faq'
get 'welcome/download'
get 'welcome/contact'

Specify a how you want the url to be and which controller and controller method to visit.
Example, in your routes.rb
get 'about', to: 'pages#about'
This way, example.com/about will point to action about in PagesController

Try this one:
scope '/', controller: 'welcome' do
get :index
get :about
get :brand
get :product
get :news
get :faq
get :download
get :contact
end
It will also allow you to forget about writing controller name in each line.

Related

Ruby on Rails - How to add a second route for the same action?

I have this route:
resources :posts do
resources :comments
end
A post can be a "User Message" showed at:
/posts
A post can be a "News" showed at:
/news
How can I create the "/news" url inside the routes.rb file and pass a param for know inside the post controller the type of post I want?
This way you can declare routes which can not be declared using resources.
get '/news' => 'posts#index', as: :news

ArgumentError: Missing :controller key on routes definition

Rails.application.routes.draw do
resources :items
root 'items#index'
get 'items/index', to:'items/index'
get 'items/show', to:'items/show'
get 'items/new', to:'items/new'
get 'items/edit', to:'items/edit'
post '/items/create', to:'items/create'
post '/items/update', to:'items/update'
post '/items/destroy', to:'items/destroy'
get '/users/userindex', to: 'users/userindex'
get '/users/usershow', to: 'users/usershow'
get '/users/usernew', to: 'users/usernew'
end
ArgumentError: Missing :controller key
The notation used to map the routes to a controller's action is controller#action, not controller/action. Change your routes accordingly
Rails.application.routes.draw do
resources :items
root 'items#index'
get 'items/index', to:'items#index'
get 'items/show', to:'items#show'
get 'items/new', to:'items#new'
get 'items/edit', to:'items#edit'
post '/items/create', to:'items#create'
post '/items/update', to:'items#update'
post '/items/destroy', to:'items#destroy'
get '/users/userindex', to: 'users#userindex'
get '/users/usershow', to: 'users#usershow'
get '/users/usernew', to: 'users#usernew'
end
Moreover, you should take a look at Resourceful Routing. You have most of the routes declared wrongly. In other words, they wouldn't be needed when you already have them with resources

Rails 4 - How to match routes in namespace

Hi i have admin panel controller and have many controller in admin panel.
I want to match routes usually without namespace i've used
match ':controller(/:action(/:id))', :via => [:get, :post]
I want this in namespace controller my current
router.rb
namespace :admin do
get '', to: 'dashboard#index', as: '/'
get 'dashboard/index'
##AUTHENTICATION
get 'login/index'
get 'login/logout'
post 'login/attempt_login'
get 'login/attempt_login'
##PAGES
get 'pages/index'
get 'pages/add_new'
get 'pages/edit'
post 'pages/create'
post 'pages/update'
post 'pages/task'
get 'pages/task'
##USERS
get 'users/index'
get 'users/edit'
get 'users/delete'
get 'users/destroy'
get 'users/update'
get 'users/add_new'
post 'users/create'
post 'users/update'
post 'users/task'
#USER GROUPS
get 'user_group/index'
get 'user_group/add_new'
get 'user_group/edit'
post 'user_group/create'
post 'user_group/update'
post 'user_group/task'
#USER GROUPS
get 'access_sections/index'
get 'access_sections/add_new'
post 'access_sections/create'
post 'access_sections/update'
post 'access_sections/task'
end
Any solution please?
You simply wrap the routes you're declaring in a namespace like so:
namespace :login do
get 'index'
get 'logout'
end
http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
For example we have orders which can be cancelled:
Following description in routes.rb
resources :orders do
post :cancel, to: 'orders/cancellations#cancel'
end
Will send request to app/controllers/orders/cancellations_contoller.rb
module Orders
class CancellationsController
def cancel
#order = Order.find(params[:id]).cancel
end
end
end
It's useful to refactor controller resourses with many service methods.
Wish it helps

Use same controller for multiple routes?

Is there a way to write the following routes so you don't have to specify the same controller each time?...
get 'jobs' => 'pages#jobs'
get 'contact' => 'pages#contact'
get 'terms' => 'pages#terms'
get 'privacy' => 'pages#privacy'
Here are couple of alternatives:
Out of the three, the first one i.e., Using scope as "/" would create the exact same routes as the ones created by the routes defined in the question.
1. Using scope as "/"
scope "/", controller: :pages do
get 'jobs'
get 'contact'
get 'terms'
get 'privacy'
end
Creates routes as below:
jobs GET /jobs(.:format) pages#jobs
contact GET /contact(.:format) pages#contact
terms GET /terms(.:format) pages#terms
privacy GET /privacy(.:format) pages#privacy
2. Using Scope as "pages"
scope :pages, controller: :pages do
get 'jobs'
get 'contact'
get 'terms'
get 'privacy'
end
Creates routes as below:
jobs GET /pages/jobs(.:format) pages#jobs
contact GET /pages/contact(.:format) pages#contact
terms GET /pages/terms(.:format) pages#terms
privacy GET /pages/privacy(.:format) pages#privacy
3. Nesting routes
resources :pages do
member do
get 'jobs'
get 'contact'
get 'terms'
get 'privacy'
end
end
Creates routes as below:
jobs_page GET /pages/:id/jobs(.:format) pages#jobs
contact_page GET /pages/:id/contact(.:format) pages#contact
terms_page GET /pages/:id/terms(.:format) pages#terms
privacy_page GET /pages/:id/privacy(.:format) pages#privacy

Rails routing. Singular resource

I've got Rails routing problem. I would like to use singular resource with user controller but it doesn't work as I expected. Here is the fragment of my routes.rb file:
scope :module => "frontend" do
root :to => "home#index"
resource :user, :controller => "user"
get "/sign_up" => "user#new"
get "/sign_in" => "user#sign_in"
get "/sign_out" => "user#sign_out"
post "/authenticate" => "user#authenticate"
resources :articles
resources :article_categories
end
I thought it will work when I'll use for example "/user" or "/user/new" URL but it didn't. I get a routing error:
No route matches {:controller=>"frontend/user"}
The 'rake routes' command output is:
user POST /user(.:format) frontend/user#create
new_user GET /user/new(.:format) frontend/user#new
edit_user GET /user/edit(.:format) frontend/user#edit
GET /user(.:format) frontend/user#show
PUT /user(.:format) frontend/user#update
DELETE /user(.:format) frontend/user#destroy
sign_up GET /sign_up(.:format) frontend/user#new
sign_in GET /sign_in(.:format) frontend/user#sign_in
sign_out GET /sign_out(.:format) frontend/user#sign_out
authenticate POST /authenticate(.:format) frontend/user#authenticate
What is interesting, when I add route for index action in user controller, like this:
scope :module => "frontend" do
root :to => "home#index"
resource :user, :controller => "user"
get "/user" => "user#index"
get "/sign_up" => "user#new"
get "/sign_in" => "user#sign_in"
get "/sign_out" => "user#sign_out"
post "/authenticate" => "user#authenticate"
resources :articles
resources :article_categories
end
...it works!
But index action is not defined in user controller!
'rake routes' command returns double line for GET /user
GET /user(.:format) frontend/user#show
GET /user(.:format) frontend/user#index
so I suppose that's not the solution. Other actions assigned to '/users' URL don't work.
Is it necessary to define the route for the index action like
get "/controller_name" => "controller_name#index"
What am I doing wrong?
Defining a singular resource in your routes will not generate a route to an index action by design. The singular resource implies you're always going to lookup this resource without specifying an ID and consequently a get to index for a singular resource just doesn't make logical sense. So, a GET to your url "/user" will route to a show action for that singular resource and not an index.
EDIT: Since your issue isn't obvious, I'd simplify your routes until you can at least hit the controller you'd expect and then build from there.
config/routes.rb
scope :module=>"frontend" do
resource :user
end
#ensure you don't have any other user routes listed before this that would match "/user".
app/controllers/frontend/users_controller.rb
module Frontend
class UsersController < ApplicationController
def show
raise "in frontend/show"
end
end
end
Thanks a lot for help! I found the bug.
The routing error was caused by the following line of the layout html file
<%= auto_discovery_link_tag(:rss, {:action => "index"}, {:title => "RSS"}) %>
I was looking for errors in the erb view files but I forgot about the layout.
I must remember to check the entire view layer in such situations.

Resources