我的form_for就是这样开始的。
<%= form_for([@memberlisting.membercontact, @memberlisting], url: backend_membercontact_memberlistings_path) do |f| %>
我遇到路由错误的编辑/更新问题。
No route matches [PATCH] "/backend/membercontacts/1/memberlistings"
通过另一个问题,方法有一个修复:: patch plus backend_membercontact_memberlisting(s)_path删除了s,我可以更新内容。
<%= form_for([@memberlisting.membercontact, @memberlisting], url: backend_membercontact_memberlisting_path, method: :patch) do |f| %>
现在show / new发出了之前没有的错误。
No route matches {:action=>"show", :controller=>"backend/memberlistings", :membercontact_id=>"1"} missing required keys: [:id]
我认为最后放置方法补丁会导致上述错误,但是如何让show / new + edit / update都能正常工作?
一些其他信息
的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
我喜欢把所有东西都按照它的方式嵌套。我无法弄清楚为什么crud函数给我带来问题,如果我拿出补丁方法它不会更新但是当我把补丁方法放入其中时不会创建一个新的或显示缺少id的b / c记录
答案 0 :(得分:1)
您的form_for
指定了URL选项,这意味着它始终使用records
参数中提供的对象路由到该路径。您列出的路径与现有对象兼容,但与new
对象不匹配,后者尚未拥有ID
。
您也无法指定PATCH,因为即使该方法不存在,它也会一直使用它。
您还需要在form_for
records
参数中包含您的命名空间,因此它看起来非常接近以下内容:
<%= form_for([:backend, @memberlisting.membercontact, @memberlisting]) do |f| %>
给它一个镜头,看它是否有效。您可以阅读有关form_for帮助程序here的更多信息。