gpt4 book ai didi

ruby-on-rails - RoR - 外键设置为 nil 后记录保存失败

转载 作者:数据小太阳 更新时间:2023-10-29 08:59:45 24 4
gpt4 key购买 nike

问题:

LocationsController request to edit should load the requested location
Failure/Error: @location.image = @image
ActiveRecord::RecordNotSaved:
Failed to remove the existing associated image.
The record failed to save when after its foreign key was set to nil.

LocationsController request to edit should be successfull
Failure/Error: @location.image = @image
ActiveRecord::RecordNotSaved:
Failed to remove the existing associated image.
The record failed to save when after its foreign key was set to nil.

Controller 的一部分:

def new
@location = Location.new
end

def create
@location = Location.new(params[:location])
@location.owner_id = current_user.id
@address = Address.new(params[:address])
@location.image = Image.new(params[:image])
responds_to_parent do
if @location.save && @location.image.update_from_uploaded_data(params[:image])
@address.addressable = @location
@address.save
@locations = Location.all_as_select_options
respond_to do |format|
format.js { render layout: false }
end
else
render :update
end
end
end

def edit
@address = @location.address
end

def update
@location.image ||= Image.new
@location.address ||= Address.new
address = params[:address]

if @location.update_attributes(params[:location]) && @location.address.update_attributes(address) && @location.image.update_from_uploaded_data(params[:image])
flash[:notice] = 'Daten für den Ort wurden geändert.'
redirect_to location_path(@location)
else
flash[:notice] = 'Die Daten konnten nicht gespeichert werden'
render action: 'edit'
end
end

def destroy
@location.destroy if @location.owner.id == current_user.id
redirect_to action: 'index', controller: 'locations'
end

private

def load_location
@location = Location.find(params[:id])
end

R规范:

describe 'request to edit' do
before do
login(mock_admin)
@location = create_location
@image = create_image
Location.stub!(:find).and_return(@location)
Image.stub!(:find).and_return(@image)
@location.image = @image
get :edit, id: @location.id
end
it 'should be successfull' do
response.should be_success
end
it 'should load the requested location' do
assigns(:location).should eql(@location)
end
it 'should load the locations address' do
assigns(:address).should eql(@location.address)
end
end

我的尝试:在我的编辑方法中允许位置和图像等参数。就像在 Can't update my nested model form for has_one association 上建议的那样

我试过了

'params.require(:...).permit(...)'

accepts_nested_attributes_for(:..., update_only: true)

建议的解决方案。但即使所有参数都允许,它也没有成功。你知道为什么吗?我试图修改这些方法,但是很好......

编辑 -- -- 模型:

class Location < ApplicationRecord
belongs_to :owner, class_name: '::User', foreign_key: 'owner_id'
has_one :address, as: :addressable, :dependent => :destroy
has_one :image, as: :visualisable
has_many :events

validates_presence_of :owner_id, :name

def display_name
[name.to_s, address&.display].compact.join(', ')
end

def self.all_as_select_options
all.collect { |m| [m.name, m.id] }
end

def can_be_deleted_by?(user)
owner == user && events.count == 0
end
end

最佳答案

您要删除图像吗?因为 无法删除现有的关联图像。 如此说。

如果是这样,请尝试将 accepts_nested_attributes_for(:..., allow_destroy: true) 添加到您的 Location 模型。

关于ruby-on-rails - RoR - 外键设置为 nil 后记录保存失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39330723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com