【问题标题】:RoR - The record failed to save when after its foreign key was set to nilRoR - 将外键设置为 nil 后记录无法保存
【发布时间】:2016-09-05 12:33:33
【问题描述】:

问题:

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.

控制器的一部分:

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

Rspec:

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

【问题讨论】:

    标签: ruby-on-rails ruby testing rspec


    【解决方案1】:

    您要删除图像吗? 因为Failed to remove the existing associated image.是这么说的。

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

    【讨论】:

    • 感谢您的回复!我试图添加 accept_nested_attributes_for(:image, allow_destroy: true) 但不知何故它没有成功。是的,我尝试删除它。我知道我必须在某个地方使用allow_destroy,但是当我将它放入模型时会出现更多错误。
    • 您能否将Location 模型的内容附加到问题中?
    • 当你添加“allow_destroy”时,你得到了什么确切的错误?您是否也像这样在params.require 中添加:_destroy 参数:params.require(:location).permit(some_location_params, image_attributes: [:file, :_destroy])
    • 添加了我的模型。我只是从我的测试中得到更多的错误。就像,我的创建功能也不再起作用了。我没有添加 :_destroy 参数。现在得试试。真的,感谢您的时间和支持!
    猜你喜欢
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多