嵌套命名空间更新操作没有路由匹配 [PATCH]

Nested Namespace Update Action No route matches [PATCH]

一直在尝试通过 rails 使用命名空间来取得进展,也有类似的问题我认为我的问题是一切都已命名空间。

我尝试将 class 添加到模型中(之前使用带有嵌套对象的设计用户进行过此操作并且有效)但在处理命名空间时它似乎不起作用。

我收到路由错误。

 No route matches [PATCH] "/backend/membercontacts/1/memberlistings"

routes.rb

# Application Client Backend
namespace :backend do
    # Member Routes
    resources :membercontacts do
        resources :memberaddresses
        resources :memberlistings
    end
end

backend/memberlisting.rb

class Backend::Memberlisting < ActiveRecord::Base

    # Model Relationships
    belongs_to :membercontact

end

backend/membercontact.rb

class Backend::Membercontact < ActiveRecord::Base

    # Model Relationship
    has_many :memberlistings, dependent: :destroy

end

backend/memberlistings_controller.rb

class Backend::MemberlistingsController < ApplicationController

    # Security & Action Filters
    layout '/backend/application.html.erb'
    before_action :set_memberlisting, only: [:show, :edit, :update, :destroy]

    # Member Listing Index
    def index
        membercontact = Backend::Membercontact.find(params[:membercontact_id])
        @memberlistings = membercontact.memberlistings.order('mlcontactname ASC')
    end

    # Detailed Member Listing Profile
    def show
        membercontact = Backend::Membercontact.find(params[:membercontact_id])
        @membercontact = membercontact.memberlistings.find(params[:id])
    end

    # New Member Listing
    def new
        membercontact = Backend::Membercontact.find(params[:membercontact_id])
        @memberlisting = membercontact.memberlistings.build
        respond_to do |format|
            format.html # new.html.erb
            format.xml  { render :xml => @membercontact }
        end
    end

    # Edit Member Listing
    def edit
        membercontact = Backend::Membercontact.find(params[:membercontact_id])
        @memberlisting = membercontact.memberlistings.find(params[:id])
    end

    # Create Member Listing Action
    def create
        @membercontact = Backend::Membercontact.find(params[:membercontact_id])
        @memberlisting = @membercontact.memberlistings.create(memberlisting_params)
        respond_to do |format|
        if @memberlisting.save
            format.html { redirect_to backend_membercontact_memberlistings_path, notice: 'Address for Membercontact was Successfully Created.' }
            format.json { render action: 'show', status: :created, location: @memberlisting }
            else
                format.html { render action: 'new' }
                format.json { render json: @memberlisting.errors, status: :unprocessable_entity }
            end
        end
    end

    # Update Member Listing Action
    def update
        respond_to do |format|
        if @memberlisting.update(memberlisting_params)
            format.html { redirect_to backend_membercontact_memberlistings_path, notice: 'Address for Membercontact was Successfully Updated.' }
            format.json { head :no_content }
            else
                format.html { render action: 'edit' }
                format.json { render json: @memberlisting.errors, status: :unprocessable_entity }
            end
        end
    end

    # Delete Member Listing Action
    def destroy
        @membercontact = Backend::Membercontact.find(params[:membercontact_id])
        @memberlisting = @membercontact.memberlistings.find(params[:id])
        @memberlisting.destroy
        respond_to do |format|
            format.html { redirect_to backend_membercontact_memberlistings_path, notice: 'Address for Membercontact was Successfully Deleted.'  }
            format.json { head :no_content }
        end
    end

    private

    def set_memberlisting
        @memberlisting = Backend::Memberlisting.find(params[:id])
    end

    def memberlisting_params
        params.require(:backend_memberlisting).permit(:mlcontactname, :mlcompanyname, :mladdressline1, :mladdressline2, :mlcity, :mlprovince, :mlpostalcode, :mlphone, :mlwebsite, :mlemail, :membercontact_id, :account_id)
    end

end

路线抽成

backend_membercontact_memberlistings_path       GET /backend/membercontacts/:membercontact_id/memberlistings(.:format)  backend/memberlistings#index
                                                POST    /backend/membercontacts/:membercontact_id/memberlistings(.:format)  backend/memberlistings#create
new_backend_membercontact_memberlisting_path    GET /backend/membercontacts/:membercontact_id/memberlistings/new(.:format)  backend/memberlistings#new
edit_backend_membercontact_memberlisting_path   GET /backend/membercontacts/:membercontact_id/memberlistings/:id/edit(.:format) backend/memberlistings#edit
backend_membercontact_memberlisting_path        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

到目前为止,我已经尝试在模型文件中使用 classes(仍在学习),并尝试了如下所示的一些变体。也是我定义url路径的原因,它默认为backend_membercontact_backend_memberlisting

, class_name: 'Backend::Memberlisting'
<%= form_for([@memberlisting.membercontact, @memberlisting], url: backend_membercontact_memberlistings_path, method: :post) do |f| %>

尝试使用

<%= form_for([@memberlisting.membercontact, @memberlisting], url: backend_membercontact_memberlisting_path, method: :post) do |f| %>

根据您的路线,您正在使用 backend_membercontact_memberlistings_path 应该是 backend_membercontact_memberlisting_path

希望这有效。

No route matches [PATCH] "/backend/membercontacts/1/memberlistings"

根据您的 rake routes 输出,路径是 backend_membercontact_memberlisting_path( no s memberlisting) 而不是 backend_membercontact_memberlistings_path

此外,您应该将 method: post 更改为 method: :patch

<%= form_for([@memberlisting.membercontact, @memberlisting], url: backend_membercontact_memberlisting_path, method: :patch) do |f| %>