How do I redirect_to a route thats inside of a namespace in rails?

66 Views Asked by At

I have a project where I am using rails as my backend and I have a model called "Projects". I am trying to do a simple redirect to my index action after deleting a single project but I keep getting errors saying that the path does not exist. I believe the error is because my controller is nested in some namespaces like so:

namespace :api do
    namespace :v1 do
      root :to => 'projects#index'
      resources :videos
      resources :projects
      resources :project_types
    end
  end

I have a root inside of the namespace, but I am unable to successfully redirect to it. I have run rails routes so I can see that the api_v1_root route exists but I don't know how to redirect to it. My delete action looks like this

def destroy
  project = Project.find(params[:id])
  project.destroy

  redirect_to :root , status: :see_other
end

and the error being returned looks like this

"status": 500,
    "error": "Internal Server Error",
    "exception": "#<NoMethodError: undefined method `root_url' for #<Api::V1::ProjectsController:0x0000000009b488>>"

I have tried :api_v1_root, and a few other variations but none have worked. So does anyone know how I can redirect correctly? Thanks!

EDIT:

I ran rails routes | grep api and got back:

api_v1_root GET    /api/v1(.:format)                                                                                 api/v1/projects#index
                           api_v1_videos GET    /api/v1/videos(.:format)                                                                          api/v1/videos#index
                                         POST   /api/v1/videos(.:format)                                                                          api/v1/videos#create
                            api_v1_video GET    /api/v1/videos/:id(.:format)                                                                      api/v1/videos#show
                                         PATCH  /api/v1/videos/:id(.:format)                                                                      api/v1/videos#update
                                         PUT    /api/v1/videos/:id(.:format)                                                                      api/v1/videos#update
                                         DELETE /api/v1/videos/:id(.:format)                                                                      api/v1/videos#destroy
                         api_v1_projects GET    /api/v1/projects(.:format)                                                                        api/v1/projects#index
                                         POST   /api/v1/projects(.:format)                                                                        api/v1/projects#create
                          api_v1_project GET    /api/v1/projects/:id(.:format)                                                                    api/v1/projects#show
                                         PATCH  /api/v1/projects/:id(.:format)                                                                    api/v1/projects#update
                                         PUT    /api/v1/projects/:id(.:format)                                                                    api/v1/projects#update
                                         DELETE /api/v1/projects/:id(.:format)                                                                    api/v1/projects#destroy
                    api_v1_project_types GET    /api/v1/project_types(.:format)                                                                   api/v1/project_types#index
                                         POST   /api/v1/project_types(.:format)                                                                   api/v1/project_types#create
                     api_v1_project_type GET    /api/v1/project_types/:id(.:format)                                                               api/v1/project_types#show
                                         PATCH  /api/v1/project_types/:id(.:format)                                                               api/v1/project_types#update
                                         PUT    /api/v1/project_types/:id(.:format)                                                               api/v1/project_types#update
                                         DELETE /api/v1/project_types/:id(.:format)                                                               api/v1/project_types#destroy

I've used api_v1_root and api_v1_root_path and both return the error <ActionController::RoutingError: No route matches [DELETE] \"/api/v1\">

0

There are 0 best solutions below