一直试图通过使用namspacing进行导航,我遇到了类似的问题,我认为我的问题就是命名空间。
我尝试在模型中添加一个类(之前使用nest对象设计并使用了它)但在处理命名空间时它似乎没有用。
我收到路由错误。
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
后端/ memberlisting.rb
class Backend::Memberlisting < ActiveRecord::Base
# Model Relationships
belongs_to :membercontact
end
后端/ membercontact.rb
class Backend::Membercontact < ActiveRecord::Base
# Model Relationship
has_many :memberlistings, dependent: :destroy
end
后端/ 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
到目前为止,我已尝试在模型文件中使用类(仍在学习)并尝试了一些变体,如下所示。也是我定义url路径的原因,它默认为backend_membercontact_backend_memberlisting
, class_name: 'Backend::Memberlisting'
<%= form_for([@memberlisting.membercontact, @memberlisting], url: backend_membercontact_memberlistings_path, method: :post) do |f| %>
答案 0 :(得分:1)
没有路线匹配[PATCH]&#34; / backend / membercontacts / 1 / memberlistings&#34;
在rake routes
输出中,路径为backend_membercontact_memberlisting_path
(无 s memberlisting
)而不是backend_membercontact_memberlistings_path
此外,您应该将method: post
更改为method: :patch
<%= form_for([@memberlisting.membercontact, @memberlisting], url: backend_membercontact_memberlisting_path, method: :patch) do |f| %>
答案 1 :(得分:0)
尝试使用
<%= form_for([@memberlisting.membercontact, @memberlisting], url: backend_membercontact_memberlisting_path, method: :post) do |f| %>
根据您的路线,您使用backend_membercontact_memberlistings_path
应该是backend_membercontact_memberlisting_path
希望这有效。