Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have one resource in routes for namespace and root path altogether - Rails 4

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?

like image 662
Petros Kyriakou Avatar asked Jul 30 '15 13:07

Petros Kyriakou


People also ask

What is the difference between resources and resource in Rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.

What is member and collection routes in Rails?

A member route will require an ID, because it acts on a member. A collection route doesn't because it acts on a collection of objects. Preview is an example of a member route, because it acts on (and displays) a single object.

What is match in Rails routes?

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

What are resources in Rails routes?

Any object that you want users to be able to access via URI and perform CRUD (or some subset thereof) operations on can be thought of as a resource. In the Rails sense, it is generally a database table which is represented by a model, and acted on through a controller.

What is resource routing in rails 2?

2 Resource Routing: the Rails Default. Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.

How do I create routes for more than one resource?

If you need to create routes for more than one resource, you can save a bit of typing by defining them all with a single call to resources: This works exactly the same as: Sometimes, you have a resource that clients always look up without referencing an ID.

How do I use the root method in rails?

You can specify what Rails should route '/' to with the root method: You should put the root route at the top of the file, because it is the most popular route and should be matched first. The root route only routes GET requests to the action. You can also use root inside namespaces and scopes as well. For example:

What is each method in resource route in rails?

Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller. When your Rails application receives an incoming request for: it asks the router to map it to a controller action. If the first matching route is:


1 Answers

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

like image 110
Cyril Duchon-Doris Avatar answered Oct 21 '22 03:10

Cyril Duchon-Doris