Hi i have a rails app and i use different namespaces in it like user, admin etc.
My url's under user namespaces as
'/user/:controller_name' but i want to
use it like
'/library/:library_id/user/:controller_name'
Users has one-to-one relationships with libraries. And i can get current users library id.
when i tried to make it with path parameter in route like
namespace :user, path: "library/user" do
...
end
it is working but i couldnt get id.
Is it possible to do this?
This does not work?
namespace :user, path: "library/:library_id/user" do
...
end
I guess, this is wat you are looking for
resources :libraries do
namespace :users do
resources :resource_name
end
end
# Output for me
/libraries/:library_id/users/controller_name/new(.:format)
/libraries/:library_id/users/controller_name/:id/edit(.:format)
.
.
This should work for you:
resources :libraries do
member do
resources :user do
collection do
resources :controller_name
end
end
end
end
Related
I am trying to place my tasks into my dashboard/admin routes, for example rather than todos/, todos/new, etc. I would like dashboard/todos/, dashboard/todos/new etc. etc.
I have tried to do that here like so;
namespace :dashboard do
resources :todos
end
Though this also changes the controller, which I don't want - I only want to nest the todos inside of the dashboard controller
Can anyone point me in the right direction please?
You can use scope or path as described in the Rails Guides
scope '/dashboard' do
resources :todos
end
or
resources :todos, path: '/dashboard/todos'
This will generate the routes with the path /dashboard/todos which maps to todos_controller
If you want to route /admin/todos to TodosController you can use scopeinstead of namespace:
scope '/admin' do
resources :todos
end
You could check the rails documentations about this point : http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
I am making a custom admin panel in a namespace "admin".
I have resources "courses" within that namespace.
But I would also like a route to "courses" that is not in that namespace:
eg: BOTH localhost:3000/admin/courses AND localhost:3000/courses
It's OK if this requires different controllers.
My concern is that its not really DRY if i have both resources for the same route.
namespace admin do
resources :courses
end
and just
resources :courses
Is there a way to have one resource be shared between namespace and without namespace, or is the example above the way to go?
Oh wait ! There's also the possibility to use concerns !
concern :shared_actions do
resources :courses
resources :something_else
end
namespace :admin do
concerns :shared_actions
end
concerns :shared_actions # Will add it to the root namespace ^^
EDIT : apparently this is what this guy also tried to do :D
I'm not really sure I understand what you mean, but
namespace :something is actually a shorthand for scope :something, module: :something, as: :something
scope :something will add /something/ as a URL prefix
scope module: :something will add /something as a controller prefix (controllers will be fetched under controlelrs/something/the_controller.rb
scope as: :something will add the something as a prefix for path helpers
Now it's totally fine to have both in your routes
resources :courses
# Will generate "/courses/", "/courses/new", "/courses/1/edit", ...
# And will point to `controllers/courses_controller.rb`
namespace :admin do
resources :courses
end
# Will generate "/admin/courses/", "/admin/courses/new", "/admin/courses/1/edit", ...
# And will point to `controllers/admin/courses_controller.rb`
Does this answer your question ?
I would like to create a resourceful route on a resource member, but I can't seem to find the syntax to create the named route that I want.
namespace :admin
resources :foobars do
get :attribute, on: :member, as: :attribute
end
end
This will provide a route method called:
attribute_admin_foobar_path
I would like it to say:
admin_foobar_attribute_path
The only other way I can think of would be to reject the resources block and create a single route:
namespace :admin
resources :foobars
get 'foobars/:id/attribute', as: :foobar_attribute
end
However, I don't like this approach because it forces me to duplicate the routing structure of already existing routes...not very DRY.
Is there a way that I can create the route name that I want while still using the resources routing block?
If you do it like this:
namespace :admin do
resources :foobars do
get :attribute
end
end
You will get:
admin_foobar_attribute GET /admin/foobars/:foobar_id/attribute(.:format) admin/foobars#attribute
That is admin_foobar_attribute_path.
Say I have a "CleaningLogEntry", "SalesLogEntry", "ServiceLogEntry" and as so on.
I understand that I can namespace the routes and have them nested. That's good. But I want them to just say log within that namespace. Is it possible
such as
resources :facilities do
resources :cleaning_log_entries
end
gives
facilities/20/cleaning_log_entries
But I want
facilities/20/logs
This is just the same concept repeated, in case it wasn't clear with some brainstorming
resources :client do
resources :sales_log_entries
end
#From
client/20/sales_log_entries
#To
client/20/logs
resources :services do
resources :services_log_entries
end
#From
services/20/services_log_entries
#To
service/20/logs
Whoops!
Figured it out. Missed this on Rails Guides:
http://guides.rubyonrails.org/routing.html#overriding-the-named-helpers
You need to add the ":as => 'desired_name'" option for:
resources :facilities do
resources :cleaning_log_entries, :as => 'logs'
end
I need some help with routes for nested controllers. I can't figure out from the Rails guide docs by myself.
I have the following controllers in a rails 3.2 app:
/app/controllers/organizations_controller.rb (class OrganizationsController)
/app/controllers/organization/events_controller.rb (class Organization::EventsController)
then, in routes.rb
resources :organizations, path: 'org' do
resources :events
member do
get 'confirm'
end
end
end
running rake routes shows (only the relevant part for my issue):
organization_event GET /org/:organization_id/events/:id(.:format) events#show
The URL is ok, the route name is also ok, but the mapping to the "controller/action" is not right. Not as I want it to be. It should be organization/events#show.
What am I missing? How can I point this route to the correct controller. I chose to put the events_controller in the organization folder, because I already have another events_controller placed in the root of the controllers folder, and they have different purposes.
Thank you
namespace :organization do
resources :events
member do
get "confirm"
end
end
end
More info here.
EDIT
Sorry, didn't understand you correctly.
resources :organizations, path: 'org' do
resources :events, :module => "organization"
member do
get 'confirm'
end
end
end
Does that fit your needs?