嵌套资源命名空间控制器

时间:2015-10-01 01:43:05

标签: ruby-on-rails ruby ruby-on-rails-4 namespaces

我构建了一个没有命名空间的应用程序,现在想尝试学习命名空间。 作为分离app api和后端的示例

尝试获得此结果 /后端/ directorycategory / 1 / Directory上市

我已经在某些地方删除了后端以尝试调试,因为我怀疑它与目录列表是嵌套的并且没有像目录类别那样的后端这一事实有关。

控制器/后端/ directorylistings_controller.rb

class Backend::DirectorylistingsController < ApplicationController
    # SECURITY
    before_action :set_backend_directorylisting, only: [:edit, :update, :destroy]

    # INDEX
    def index
    backend_directorycategory = Backend::Directorycategory.find(params[:directorycategory_id])
    @backend_directorylistings = backend_directorycategory.backend_directorylistings
    end

    # SHOW
    def show
        backend_directorycategory = Backend::Directorycategory.find(params[:directorycategory_id])
        @backend_directorycategory = backend_directorycategory.backend_directorylistings.find(params[:id])
    end

    def new
        backend_directorycategory = Backend::Directorycategory.find(params[:directorycategory_id])
        @directorylisting = backend_directorycategory.directorylistings.build
        respond_to do |format|
            format.html # new.html.erb
            format.xml  { render :xml => @backend_directorycategory }
        end
    end

    # EDIT
    def edit
        backend_directorycategory = Backend::Directorycategory.find(params[:directorycategory_id])
        @directorylisting = backend_directorycategory.directorylistings.find(params[:id])
    end

    # CREATE
    def create
        @backend_directorycategory = Backend::Directorycategory.find(params[:directorycategory_id])
        @directorylisting = @backend_directorycategory.directorylistings.create(directorylisting_params)
        respond_to do |format|
        if @directorylisting.save
            format.html { redirect_to backend_directorycategory_directorylistings_path, notice: 'Directory Listing was Successfully Created.' }
            format.json { render action: 'show', status: :created, location: @directorylisting }
            else
                format.html { render action: 'new' }
                format.json { render json: @backend_directorylisting.errors, status: :unprocessable_entity }
            end
        end
    end

    # UPDATE
    def update
        respond_to do |format|
        if @directorylisting.update(directorylisting_params)
            format.html { redirect_to backend_directorycategory_directorylistings_path, notice: 'Directory Listing was Successfully Updated.' }
            format.json { head :no_content }
            else
                format.html { render action: 'edit' }
                format.json { render json: @directorylisting.errors, status: :unprocessable_entity }
            end
        end
    end

    # DELETE
    def destroy
        @backend_directorycategory = Directorycategory.find(params[:directorycategory_id])
        @directorylisting = @backend_directorycategory.directorylistings.find(params[:id])
        @directorylisting.destroy
        respond_to do |format|
            format.html { redirect_to backend_directorycategory_directorylistings_path, notice: 'Directory Listing was Successfully Deleted.'  }
            format.json { head :no_content }
        end
    end

    private

  def set_backend_directorylisting
        @directorylisting = Directorylisting.find(params[:id])
    end

  def directorylisting_backend_params
    params.require(:backend_directorylisting).permit(:memberlisting_id, :directorycategory_id)
    end

end

模型/后端/ directorylisting.rb

class Backend::Directorylisting < ActiveRecord::Base

    # Model Relationship
  belongs_to :backend_directorycategory
  belongs_to :backend_memberlisting
  has_one :backend_membercontact, :through => :backend_memberlisting

    # Validation for Form Fields (More of a Self-Check)
    validates :memberlisting_id, 
        presence: true

    # Validation for Form Fields (More of a Self-Check)
    validates :directorycategory_id, 
        presence: true

end

/models/backend.rb

module Backend
    def self.table_name_prefix
        'backend_'
    end
end

的routes.rb

# Active Security + Data Separation
Rails.application.routes.draw do

    # Security + Data Separation

    namespace :api do
            resources :directorycategories, defaults: {format: 'json'} do
                resources :directorylistings
            end
    end

  namespace :backend do
            # Member Routes
            resources :membercontacts do
                resources :memberlistings
            end

            # Directory Information
            resources :directorycategories do
                resources :directorylistings
            end
        end 

    # Mount Engine @ Root

end

Rake route(namespaced)

Prefix                                      Verb   URI                                                                                 Pattern                                                                      Controller#Action
api_directorycategory_directorylistings     GET    /api/directorycategories/:directorycategory_id/directorylistings(.:format)              api/directorylistings#index {:format=>"json"}
                                            POST   /api/directorycategories/:directorycategory_id/directorylistings(.:format)              api/directorylistings#create {:format=>"json"}
new_api_directorycategory_directorylisting  GET    /api/directorycategories/:directorycategory_id/directorylistings/new(.:format)          api/directorylistings#new {:format=>"json"}
edit_api_directorycategory_directorylisting GET    /api/directorycategories/:directorycategory_id/directorylistings/:id/edit(.:format)     api/directorylistings#edit {:format=>"json"}
api_directorycategory_directorylisting      GET    /api/directorycategories/:directorycategory_id/directorylistings/:id(.:format)          api/directorylistings#show {:format=>"json"}
                                            PATCH  /api/directorycategories/:directorycategory_id/directorylistings/:id(.:format)          api/directorylistings#update {:format=>"json"}
                                            PUT    /api/directorycategories/:directorycategory_id/directorylistings/:id(.:format)          api/directorylistings#update {:format=>"json"}
                                            DELETE /api/directorycategories/:directorycategory_id/directorylistings/:id(.:format)          api/directorylistings#destroy {:format=>"json"}
api_directorycategories                     GET    /api/directorycategories(.:format)                                                      api/directorycategories#index {:format=>"json"}
                                            POST   /api/directorycategories(.:format)                                                      api/directorycategories#create {:format=>"json"}
new_api_directorycategory                   GET    /api/directorycategories/new(.:format)                                                  api/directorycategories#new {:format=>"json"}
edit_api_directorycategory                  GET    /api/directorycategories/:id/edit(.:format)                                             api/directorycategories#edit {:format=>"json"}
api_directorycategory                       GET    /api/directorycategories/:id(.:format)                                                  api/directorycategories#show {:format=>"json"}
                                            PATCH  /api/directorycategories/:id(.:format)                                                  api/directorycategories#update {:format=>"json"}
                                            PUT    /api/directorycategories/:id(.:format)                                                  api/directorycategories#update {:format=>"json"}
                                            DELETE /api/directorycategories/:id(.:format)                                                  api/directorycategories#destroy {:format=>"json"}
backend_membercontact_memberlistings        GET    /backend/membercontacts/:membercontact_id/memberlistings(.:format)                      backend/memberlistings#index
                                            POST   /backend/membercontacts/:membercontact_id/memberlistings(.:format)                      backend/memberlistings#create
new_backend_membercontact_memberlisting     GET    /backend/membercontacts/:membercontact_id/memberlistings/new(.:format)                  backend/memberlistings#new
edit_backend_membercontact_memberlisting    GET    /backend/membercontacts/:membercontact_id/memberlistings/:id/edit(.:format)             backend/memberlistings#edit
backend_membercontact_memberlisting         GET    /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format)                  backend/memberlistings#show
                                            PATCH  /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format)                  backend/memberlistings#update
                                            PUT    /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format)                  backend/memberlistings#update
                                            DELETE /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format)                  backend/memberlistings#destroy
backend_membercontacts                      GET    /backend/membercontacts(.:format)                                                       backend/membercontacts#index
                                            POST   /backend/membercontacts(.:format)                                                       backend/membercontacts#create
new_backend_membercontact                   GET    /backend/membercontacts/new(.:format)                                                   backend/membercontacts#new
edit_backend_membercontact                  GET    /backend/membercontacts/:id/edit(.:format)                                              backend/membercontacts#edit
backend_membercontact                       GET    /backend/membercontacts/:id(.:format)                                                   backend/membercontacts#show
                                            PATCH  /backend/membercontacts/:id(.:format)                                                   backend/membercontacts#update
                                            PUT    /backend/membercontacts/:id(.:format)                                                   backend/membercontacts#update
                                            DELETE /backend/membercontacts/:id(.:format)                                                   backend/membercontacts#destroy
backend_directorycategory_directorylistings GET    /backend/directorycategories/:directorycategory_id/directorylistings(.:format)          backend/directorylistings#index
                                            POST   /backend/directorycategories/:directorycategory_id/directorylistings(.:format)          backend/directorylistings#create
new_backend_directorycategory_directorylisting  GET    /backend/directorycategories/:directorycategory_id/directorylistings/new(.:format)      backend/directorylistings#new
edit_backend_directorycategory_directorylisting GET    /backend/directorycategories/:directorycategory_id/directorylistings/:id/edit(.:format) backend/directorylistings#edit
backend_directorycategory_directorylisting  GET    /backend/directorycategories/:directorycategory_id/directorylistings/:id(.:format)      backend/directorylistings#show
                                            PATCH  /backend/directorycategories/:directorycategory_id/directorylistings/:id(.:format)      backend/directorylistings#update
                                            PUT    /backend/directorycategories/:directorycategory_id/directorylistings/:id(.:format)      backend/directorylistings#update
                                            DELETE /backend/directorycategories/:directorycategory_id/directorylistings/:id(.:format)      backend/directorylistings#destroy
backend_directorycategories                 GET    /backend/directorycategories(.:format)                                                  backend/directorycategories#index
                                            POST   /backend/directorycategories(.:format)                                                  backend/directorycategories#create
new_backend_directorycategory               GET    /backend/directorycategories/new(.:format)                                              backend/directorycategories#new
edit_backend_directorycategory              GET    /backend/directorycategories/:id/edit(.:format)                                         backend/directorycategories#edit
backend_directorycategory                   GET    /backend/directorycategories/:id(.:format)                                              backend/directorycategories#show
                                            PATCH  /backend/directorycategories/:id(.:format)                                              backend/directorycategories#update
                                            PUT    /backend/directorycategories/:id(.:format)                                              backend/directorycategories#update
                                            DELETE /backend/directorycategories/:id(.:format)                                              backend/directorycategories#destroy

rake route(:module)

Prefix                                      Verb   URI                                                                                 Pattern                                                                      Controller#Action
api_directorycategory_directorylistings     GET    /api/directorycategories/:directorycategory_id/directorylistings(.:format)              api/directorylistings#index {:format=>"json"}
                                            POST   /api/directorycategories/:directorycategory_id/directorylistings(.:format)              api/directorylistings#create {:format=>"json"}
new_api_directorycategory_directorylisting  GET    /api/directorycategories/:directorycategory_id/directorylistings/new(.:format)          api/directorylistings#new {:format=>"json"}
edit_api_directorycategory_directorylisting GET    /api/directorycategories/:directorycategory_id/directorylistings/:id/edit(.:format)     api/directorylistings#edit {:format=>"json"}
api_directorycategory_directorylisting      GET    /api/directorycategories/:directorycategory_id/directorylistings/:id(.:format)          api/directorylistings#show {:format=>"json"}
                                            PATCH  /api/directorycategories/:directorycategory_id/directorylistings/:id(.:format)          api/directorylistings#update {:format=>"json"}
                                            PUT    /api/directorycategories/:directorycategory_id/directorylistings/:id(.:format)          api/directorylistings#update {:format=>"json"}
                                            DELETE /api/directorycategories/:directorycategory_id/directorylistings/:id(.:format)          api/directorylistings#destroy {:format=>"json"}
api_directorycategories                     GET    /api/directorycategories(.:format)                                                      api/directorycategories#index {:format=>"json"}
                                            POST   /api/directorycategories(.:format)                                                      api/directorycategories#create {:format=>"json"}
new_api_directorycategory                   GET    /api/directorycategories/new(.:format)                                                  api/directorycategories#new {:format=>"json"}
edit_api_directorycategory                  GET    /api/directorycategories/:id/edit(.:format)                                             api/directorycategories#edit {:format=>"json"}
api_directorycategory                       GET    /api/directorycategories/:id(.:format)                                                  api/directorycategories#show {:format=>"json"}
                                            PATCH  /api/directorycategories/:id(.:format)                                                  api/directorycategories#update {:format=>"json"}
                                            PUT    /api/directorycategories/:id(.:format)                                                  api/directorycategories#update {:format=>"json"}
                                            DELETE /api/directorycategories/:id(.:format)                                                  api/directorycategories#destroy {:format=>"json"}
membercontact_memberlistings        GET    membercontacts/:membercontact_id/memberlistings(.:format)                      backend/memberlistings#index
                                            POST   membercontacts/:membercontact_id/memberlistings(.:format)                      backend/memberlistings#create
new_membercontact_memberlisting     GET    membercontacts/:membercontact_id/memberlistings/new(.:format)                  backend/memberlistings#new
edit_membercontact_memberlisting    GET    membercontacts/:membercontact_id/memberlistings/:id/edit(.:format)             backend/memberlistings#edit
membercontact_memberlisting         GET    membercontacts/:membercontact_id/memberlistings/:id(.:format)                  backend/memberlistings#show
                                            PATCH  membercontacts/:membercontact_id/memberlistings/:id(.:format)                  backend/memberlistings#update
                                            PUT    membercontacts/:membercontact_id/memberlistings/:id(.:format)                  backend/memberlistings#update
                                            DELETE membercontacts/:membercontact_id/memberlistings/:id(.:format)                  backend/memberlistings#destroy
membercontacts                      GET    membercontacts(.:format)                                                       backend/membercontacts#index
                                            POST   membercontacts(.:format)                                                       backend/membercontacts#create
new_membercontact                   GET    membercontacts/new(.:format)                                                   backend/membercontacts#new
edit_membercontact                  GET    membercontacts/:id/edit(.:format)                                              backend/membercontacts#edit
membercontact                       GET    membercontacts/:id(.:format)                                                   backend/membercontacts#show
                                            PATCH  membercontacts/:id(.:format)                                                   backend/membercontacts#update
                                            PUT    membercontacts/:id(.:format)                                                   backend/membercontacts#update
                                            DELETE membercontacts/:id(.:format)                                                   backend/membercontacts#destroy
directorycategory_directorylistings GET    directorycategories/:directorycategory_id/directorylistings(.:format)          backend/directorylistings#index
                                            POST   directorycategories/:directorycategory_id/directorylistings(.:format)          backend/directorylistings#create
new_directorycategory_directorylisting  GET    directorycategories/:directorycategory_id/directorylistings/new(.:format)      backend/directorylistings#new
edit_directorycategory_directorylisting GET    directorycategories/:directorycategory_id/directorylistings/:id/edit(.:format) backend/directorylistings#edit
directorycategory_directorylisting  GET    directorycategories/:directorycategory_id/directorylistings/:id(.:format)      backend/directorylistings#show
                                            PATCH  directorycategories/:directorycategory_id/directorylistings/:id(.:format)      backend/directorylistings#update
                                            PUT    directorycategories/:directorycategory_id/directorylistings/:id(.:format)      backend/directorylistings#update
                                            DELETE directorycategories/:directorycategory_id/directorylistings/:id(.:format)      backend/directorylistings#destroy
directorycategories                 GET    directorycategories(.:format)                                                  backend/directorycategories#index
                                            POST   directorycategories(.:format)                                                  backend/directorycategories#create
new_directorycategory               GET    directorycategories/new(.:format)                                              backend/directorycategories#new
edit_directorycategory              GET    directorycategories/:id/edit(.:format)                                         backend/directorycategories#edit
directorycategory                   GET    directorycategories/:id(.:format)                                              backend/directorycategories#show
                                            PATCH  directorycategories/:id(.:format)                                              backend/directorycategories#update
                                            PUT    directorycategories/:id(.:format)                                              backend/directorycategories#update
                                            DELETE directorycategories/:id(.:for

1 个答案:

答案 0 :(得分:0)

(如果你可以运行&#39; rake routes&#39;并发布输出,这可能会有所帮助)

您是否尝试在routes.rb中添加模块?我认为这可能接近你正在寻找的结果......

        resources :directorycategories, module: "backend" do
            resources :directorylistings
        end

或者您可以直接指定路径......

resources :directorycategories, path: "/backend/directorycategories" do
            resources :directorylistings
        end

我对Rails相当新,所以我不是100%,但我不知道你的控制器是否需要所有额外的代码。 (您的控制器文件结构可能会更清晰地显示您正在尝试做的事情)

希望这会有所帮助。 :)