我有一個提供 Vue 前端的 Rails Api。我有兩個主要模型,Contacts 和 Outlets,通過連接表 ContactOutlets 建立多對多關系。我試圖弄清楚如何在聯系人控制器中將關聯添加到插座。
我知道,我可以單獨呼叫 ContactOutlet create 操作,但是如果 Rails 可以在后端處理這個操作似乎很浪費。我想讓 vue 呼叫一次contact#update。
聯系方式:
class Contact < ApplicationRecord
has_many :contact_outlets
has_many :outlets, through: :contact_outlets
has_many :calls
validates_uniqueness_of :email
validates_uniqueness_of :name
end
出口型號:
class Outlet < ApplicationRecord
has_many :contact_outlets
has_many :contacts, through: :contact_outlets
has_many :calls
validates_uniqueness_of :website
end
聯系方式:
class ContactOutlet < ApplicationRecord
belongs_to :contact
belongs_to :outlet
validates_uniqueness_of :contact_id, :scope => :outlet_id
end
聯系人控制器:
class ContactsController < ApplicationController
before_action :set_contact, only: %i[ show update destroy ]
# GET /contacts
def index
@contacts = Contact.all
render json: @contacts, include: :outlets
end
# GET /contacts/1
def show
render json: @contact, include: :outlets
end
# POST /contacts
def create
@contact = Contact.new(contact_params)
if @contact.save
render json: @contact, status: :created, location: @contact
else
render json: @contact.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /contacts/1
def update
if @contact.update(contact_params)
render json: @contact, include: :outlets
else
render json: @contact.errors, status: :unprocessable_entity
end
end
# DELETE /contacts/1
def destroy
@contact.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_contact
@contact = Contact.find(params[:id])
end
# Only allow a list of trusted parameters through.
def contact_params
params.require(:contact).permit(:name, :email, :bio, :image_url)
end
end
uj5u.com熱心網友回復:
解決了這個問題。如果其他人在看上面的模型就可以了。對 contact_params 進行了一些調整,以允許訪問 outlets 陣列。然后修復了更新操作。完整的控制器代碼如下:
class ContactsController < ApplicationController
before_action :set_contact, only: %i[ show update destroy ]
# GET /contacts
def index
@contacts = Contact.all
render json: @contacts, include: :outlets
end
# GET /contacts/1
def show
render json: @contact, include: :outlets
end
# POST /contacts
def create
@contact = Contact.new(contact_params)
if @contact.save
render json: @contact, status: :created, location: @contact
else
render json: @contact.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /contacts/1
def update
if @contact.outlets
@contact.outlets.delete_all
end
if params[:outlets]
contactOutlets = params[:outlets]
contactOutlets.each do |outlet|
@contact.outlets << Outlet.find(outlet[:key])
end
end
if @contact.update(contact_params)
render json: @contact, include: :outlets
else
render json: @contact.errors, status: :unprocessable_entity
end
end
# DELETE /contacts/1
def destroy
@contact.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_contact
@contact = Contact.find(params[:id])
end
# Only allow a list of trusted parameters through.
def contact_params
params.require(:contact).permit(:name, :email, :bio, :image_url, outlet_ids:[])
end
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/441500.html
