Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

Rails routing to nested controller

I have one controller for creating category, and I want to have one more nested controller to create language versions of categories. I want to avoid methods like new_language, edit_language, ... and routing on them, I would like to do it the best rails way. But I am new to rails (from Padrino and Sinatra), and I am little bit lost in routing.

I have my controller for categories

module Admin
  class CategoriesController < ApplicationController
    before_action :authenticate_user!
    before_action :find_category, only: [:show, :edit, :update]

    layout 'admin'

    def index
      @categories = Category.all
    end

    ..... all the others CRUD methods ....

  end
end

And another controller.

module Admin
  class CategoriesLanguageController < ApplicationController
    before_action :authenticate_user!
    before_action :find_category, only: [:show, :edit, :update]

    def new
      @category = Category.find(params[:category].to_i)
    end

    .... all the others CRUD methods ...

  end
end

and my routes config.

Rails.application.routes.draw do
  devise_for :users
  resources :users
  namespace :admin do
    get '/' => 'dashboard#index'
    resources :dashboard, only: [:index]
    resources :categories do
      collection do
        get :publish_category
        #get :new_lang
        #post :create_lang
        #get :edit
        #post :update
        #get :destroy
      end
      resources :language, only: [:new, :create, :edit]
    end
  end

  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root to: "homepage#index"

  namespace :api, defaults: { format: 'json' } do
    namespace :v1 do
      resources :categories, only: [:index]
    end
  end


end

I would like to have methods new_lang and create_lang under second controller with default names new, create, ....

It generates this

                                                admin GET    /admin(.:format)                                                                                  admin/dashboard#index
                   admin_dashboard_index GET    /admin/dashboard(.:format)                                                                        admin/dashboard#index
       publish_category_admin_categories GET    /admin/categories/publish_category(.:format)                                                      admin/categories#publish_category
           admin_category_language_index POST   /admin/categories/:category_id/language(.:format)                                                 admin/language#create
             new_admin_category_language GET    /admin/categories/:category_id/language/new(.:format)                                             admin/language#new
            edit_admin_category_language GET    /admin/categories/:category_id/language/:id/edit(.:format)                                        admin/language#edit
                        admin_categories GET    /admin/categories(.:format)                                                                       admin/categories#index
                                         POST   /admin/categories(.:format)                                                                       admin/categories#create
                      new_admin_category GET    /admin/categories/new(.:format)                                                                   admin/categories#new
                     edit_admin_category GET    /admin/categories/:id/edit(.:format)                                                              admin/categories#edit
                          admin_category GET    /admin/categories/:id(.:format)                                                                   admin/categories#show
                                         PATCH  /admin/categories/:id(.:format)                                                                   admin/categories#update
                                         PUT    /admin/categories/:id(.:format)                                                                   admin/categories#update
                                         DELETE /admin/categories/:id(.:format)                                                                   admin/categories#destroy

but it is not working.

ActionController::UrlGenerationError in Admin::Categories#index

and error is this route:

<td><%= link_to cat.internal_name, edit_admin_category_language_path(id: cat) %></td>

And I don't know how to create these actions in another controller and use url helpers.

Or this is not the best way to do it?

Please any advice?

question from:https://stackoverflow.com/questions/66060385/rails-routing-to-nested-controller

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...