Tzi*_*zio 5 ruby testing rspec ruby-on-rails
问题:
\n\nLocationsController request to edit should load the requested location\nFailure/Error: @location.image = @image\n ActiveRecord::RecordNotSaved:\n Failed to remove the existing associated image. \nThe record failed to save when after its foreign key was set to nil.\n\nLocationsController request to edit should be successfull\nFailure/Error: @location.image = @image\n ActiveRecord::RecordNotSaved:\n Failed to remove the existing associated image. \nThe record failed to save when after its foreign key was set to nil.\nRun Code Online (Sandbox Code Playgroud)\n\n控制器部分:
\n\ndef new\n @location = Location.new\nend\n\ndef create\n @location = Location.new(params[:location])\n @location.owner_id = current_user.id\n @address = Address.new(params[:address])\n @location.image = Image.new(params[:image])\n responds_to_parent do\n if @location.save && @location.image.update_from_uploaded_data(params[:image])\n @address.addressable = @location\n @address.save\n @locations = Location.all_as_select_options\n respond_to do |format|\n format.js { render layout: false }\n end\n else\n render :update\n end\n end\nend\n\ndef edit\n @address = @location.address\nend\n\ndef update\n @location.image ||= Image.new\n @location.address ||= Address.new\n address = params[:address]\n\n if @location.update_attributes(params[:location]) && @location.address.update_attributes(address) && @location.image.update_from_uploaded_data(params[:image])\n flash[:notice] = \'Daten f\xc3\xbcr den Ort wurden ge\xc3\xa4ndert.\'\n redirect_to location_path(@location)\n else\n flash[:notice] = \'Die Daten konnten nicht gespeichert werden\'\n render action: \'edit\'\n end\nend\n\ndef destroy\n @location.destroy if @location.owner.id == current_user.id\n redirect_to action: \'index\', controller: \'locations\'\nend\n\nprivate\n\ndef load_location\n @location = Location.find(params[:id])\nend\nRun Code Online (Sandbox Code Playgroud)\n\n规格:
\n\ndescribe \'request to edit\' do\n before do\n login(mock_admin)\n @location = create_location\n @image = create_image\n Location.stub!(:find).and_return(@location)\n Image.stub!(:find).and_return(@image)\n @location.image = @image\n get :edit, id: @location.id\n end\n it \'should be successfull\' do\n response.should be_success\n end\n it \'should load the requested location\' do\n assigns(:location).should eql(@location)\n end\n it \'should load the locations address\' do\n assigns(:address).should eql(@location.address)\n end\n end\nRun Code Online (Sandbox Code Playgroud)\n\n我的尝试:在我的编辑方法中允许使用位置和图像等参数。就像Can't update mynested model form for has_one Association 中所建议的那样
\n\n我尝试过
\n\n\'params.require(:...).permit(...)\'\nRun Code Online (Sandbox Code Playgroud)\n\n和
\n\naccepts_nested_attributes_for(:..., update_only: true)\nRun Code Online (Sandbox Code Playgroud)\n\n建议的解决方案。但即使所有参数都允许,它也没有成功。你知道为什么吗?我尝试修改方法,但是好吧......
\n\n编辑 -- -- 型号:
\n\nclass Location < ApplicationRecord\n belongs_to :owner, class_name: \'::User\', foreign_key: \'owner_id\'\n has_one :address, as: :addressable, :dependent => :destroy\n has_one :image, as: :visualisable\n has_many :events\n\n validates_presence_of :owner_id, :name\n\n def display_name\n [name.to_s, address&.display].compact.join(\', \')\n end\n\n def self.all_as_select_options\n all.collect { |m| [m.name, m.id] }\n end\n\n def can_be_deleted_by?(user)\n owner == user && events.count == 0\n end\nend\nRun Code Online (Sandbox Code Playgroud)\n
您是否正在尝试删除该图像?因为Failed to remove the existing associated image.这么说。
如果是这样,请尝试添加accepts_nested_attributes_for(:..., allow_destroy: true)到您的Location模型中。
| 归档时间: |
|
| 查看次数: |
8158 次 |
| 最近记录: |