I want to do something like:
namespace :dashboard do
get 'speed'
get 'engine'
get 'oil'
get 'errors', :to => 'warn_system#errors', :module => false
end
Only errors link to another controller.
dashboard_speed GET /dashboard/speed(.:format) dashboard#speed
dashboard_oil GET /dashboard/oil(.:format) dashboard#oil
dashboard_engine GET /dashboard/engine(.:format) dashboard#engine
dashboard_errors GET /dashboard/errors(.:format) dashboard/warn_system#errors {:module=>false}
For the last record, I want it to be
dashboard_errors GET /dashboard/errors(.:format) warn_system#errors
What shall I do?
I am using Rails 3 if it matters.
For Rails 3, try this:
scope '/dashboard' do
get 'errors', :to => 'warn_system#errors'
end
To route to a different controller within a namespace specify the absolute path to the controller. If the warn_system controller is in the root namespace use:
namespace :dashboard do
get 'errors', :to => '/warn_system#errors'
end
Update:
Based on your comment it looks like you want to use:
namespace :dashboard do
get 'errors', :to => '/dashboard/warn_system#errors'
end
Related
Suppose I have a model User and I want to add some dashboard namespace. So I create dashbord directory and put inside private_users_controller.rb. Now for routing I put
namespace "dashboard" do
resources :users do
member do
get "show" => "private_users#show"
end
end
end
the problem is that I only want to route the get request having this route /dashboard/users/:id/show. But rake routes shows a bunch of post, delete... routes.
How can I cut those ?
seems like you don't need any of the method from resources definition, so just add a match will be ok.
namespace "dashboard" do
match 'users/:id/show', :to => 'private_users#show'
end
if you insist using resource, then the following will work
scope '/dashboard' do
resources :users, :only => :show, :module => 'private'
end
the 'rake routes' output is like this
GET /dashboard/users/:id(.:format) private/users#show
the trailing 'show' inside the url is not needed.
namespace "dashboard" do
get "users/:id/show" => "private_users#show"
end
I'm newbie on rails and getting an error while try to add new method on my controller :(
I have a controller under admin path;
Admin::MyUsersController < ApplicationController
before_filter :......
def index
redirect_to :action => :show_my_action
end
def show_my_action
...
...
end
My controller like this but not this exactly.
In my routes.rb
namespace "admin" do
resources :my_users do
get "show_my_action"
end
end
When my routes.rb is like this, im getting error => No route matches {:action=>"show_my_action", :controller=>"admin/my_users"}
namespace "admin" do
resources :my_users do
get "show_my_action", :on => :collection
end
end
when my routes.rb like this then no error :S
Why im getting this error. I can use first declaration for other controllers which is on root path.
You are adding actions to RESTful actions, if you don't specify a collection, or a member, the route can't know what you want. If you define like this:
namespace "admin" do
resources :my_users do
get "show_my_action"
end
end
How can routes know which route you want:
my_users/show_my_action, or my_users/:id/show_my_action
So, you need to specify it's member or collection:
namespace "admin" do
resources :my_users do
get "show_my_action", :on => :collection
end
end
will have route: my_users/show_my_action, and:
namespace "admin" do
resources :my_users do
get "show_my_action", :on => :member
end
en
will have route: my_users/:id/show_my_action
You can check at Adding More RESTful Actions.
You need to specify whether the action is on a member or a collection. If it's on a member then your URL is admin/my_users/:id/show_my_action. If it's on a collection then it's admin/my_users/show_my_action. Read up on it here: http://edgeguides.rubyonrails.org/routing.html
I need help. I want administration for my rails application. I tried to set the routes with namespaces, but namespaces require a resource, and resource must have id in get request.
Anybody know how to set up correctly? I using windows machine. Thanks.
My routes :
Web::Application.routes.draw do
namespace :admin do
resources :access # GET http://localhost/admin/access/login/login - stupid??
end
match ':controller(/:action(/:id))(.:format)'
end
Try to use resource :access instead of resources :access
namespace :admin do
resource :access
end
It will generate routes:
admin_access POST /admin/access(.:format) admin/access#create
new_admin_access GET /admin/access/new(.:format) admin/access#new
edit_admin_access GET /admin/access/edit(.:format) admin/access#edit
GET /admin/access(.:format) admin/access#show
PUT /admin/access(.:format) admin/access#update
DELETE /admin/access(.:format) admin/access#destroy
namespace :admin do
get "login" => "access#login", :as => :login # GET http://localhost/admin/login - admin_login_path
end
If you don't have a set of restful resources, but just want a set of different controller methods, here's one way you can do it:
scope '/admin' do
get '' => "admin#index", :as => 'admin_home'
get '/users' => 'admin#users', :as => 'admin_users'
get '/other_admin_task' => 'admin#other_admin_task', :as => 'other_admin_task'
end
With routing in Rails 3, using a namespaced route as in the following example...
namespace :admin do
get 'dashboard' => 'dashboard#index'
end
...how can I get '/admin' to route to 'dashboard#index' as well as '/admin/dashboard'? Would the best way to do it be to define...
get 'admin' => 'admin/dashboard#index'
outside the namespace or is there a more elegant way to alias a resource?
You can make the path just / which gets stripped internally by the Rails router, and just becomes /admin. The only difference is it being within your namespace instead of outside of it.
namespace :admin do
get 'dashboard' => 'dashboard#index'
get '/' => 'dashboard#index'
end
Which produces:
admin_dashboard GET /admin/dashboard(.:format) {:action=>"index", :controller=>"admin/dashboard"}
admin GET /admin(.:format) {:controller=>"admin/dashboard", :action=>"index"}
You can also do a redirect with the built in redirect method:
namespace :admin do
get 'dashboard' => 'dashboard#index'
get '/' => redirect('/admin/dashboard')
end
Or if you want to do it outside of the namespace:
get '/admin' => redirect('/admin/dashboard')
I personally like the first example best. Keeps it within the namespace and looks very similar to the default root route so it's easy to read over while working within the Admin namespaced routes.
In Rails 4 I use:
namespace :admin do
root 'dashboard#index'
end
And you can also define your custom route for /admin/dashbaord:
namespace :admin do
root 'dashboard#index'
get 'dashboard' => 'dashboard#index'
end
I have a few reoccurring patterns in my routes.rb and I would like to make it DRY by creating a method that creates those routes for me.
An example of what I want to accomplish can be seen in the Devise gem where you can use the following syntax:
#routes.rb
devise_for :users
Which will generate all the routes necessary for Devise. I would like to create something similar. Say for example that I have the following routes:
resources :posts do
member do
get 'new_file'
post 'add_file'
end
match 'files/:id' => 'posts#destroy_file', :via => :delete, :as => :destroy_file
end
resources :articles do
member do
get 'new_file'
post 'add_file'
end
match 'files/:id' => 'articles#destroy_file', :via => :delete, :as => :destroy_file
end
This starts getting messy quite quickly so I would like to find a way to do it like this instead:
resources_with_files :posts
resources_with_files :articles
So my question is, how can I create the resources_with_files method?
Put this in something like lib/routes_helper.rb:
class ActionDispatch::Routing::Mapper
def resources_with_files(*resources)
resources.each do |r|
Rails.application.routes.draw do
resources r do
member do
get 'new_file'
post 'add_file'
delete 'files' => :destroy_file
end
end
end
end
end
end
and require it in config/routes.rb