0

I'm switching from using a laravel php framework to a rails framework and cannot figure out how to get nested routes working in rails like it did in laravel. In laravel it would work like this:

Route::group(['prefix' => 'healthandwelness'], function() {

  Route::get('', [
    'uses' => 'healthandwelness@index'
  ]);

  Route::group(['prefix' => 'healthcare'], function() {

    Route::get('', [
      'uses' => 'healthcare@index'
    ]);

    Route::get('{article_id}', [
      'uses' => 'article@index'
    ]);  

  ]);

});

Which would give me the routes:

/healthandwellness
/healthandwellness/healthcare
/healthandwellness/healthcare/{article_id}

The only way I've seen to nest routes in rails is with resources which I've used to tried and recreate my routes in rails like this:

resources :healthandwellness do
  resources :healthcare 
end

But the nature of resources makes this route into the following gets:

/healthandwellness
/healthandwellness/:health_and_wellness_id/healthcare
/healthandwellness/:health_and_wellness_id/healthcare/:id

Is there anyway to do what I am looking for in rails and recreate what I was able to do laravel?

user3688241
  • 3,015
  • 2
  • 14
  • 13

1 Answers1

3

You can use namespace:

namespace :healthandwellness do
  resources :healthcare
end

Note that this assumes that your controller is located in app/controllers/healthsandwellness/healthcare_controller.rb and should be defined as follows:

class Healthandwellness::HealthcareController < ApplicationController
  ...
end

If you want to keep your controller under apps/controller and not scoped under app/controllers/healthandwellness, you can use scope:

scope '/healthandwellness' do
  resources :healthcare
end

The documentation here provides good examples of namespace and scope

AbM
  • 7,326
  • 2
  • 25
  • 28