2017-11-15 1 views
-1
Rails 5.1 

fw_export.rb 모델 롤링하지 :모델 DB에 저장하고 다시

class FwExport < ApplicationRecord 
    include Shared 

    has_one :location 

end 

location.rb 모델 :

class Location < ApplicationRecord 
    include Shared 

    belongs_to :fw_export 

end 

locations_controller.rb :

class LocationsController < ApplicationController 
    before_action :set_location, only: [:show, :edit, :update, :destroy] 

    def new 
    @location = Location.new 
    end 

    def create 
    @location = Location.new(location_params) 

    respond_to do |format| 
     if @location.save 
     format.html { redirect_to @location, notice: 'Location was successfully created.' } 
     format.json { render :show, status: :created, location: @location } 
     else 
     format.html { render :new } 
     format.json { render json: @location.errors, status: :unprocessable_entity } 
     end 
    end 
    end 


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

    def location_params 
     params.require(:location).permit(:fw_exports_id, :city, :state, :country, :api_source) 
    end 
end 

위치를 이전 파일 :

그것은 실패하고 롤백됩니다

location_record = Location.new(
    :fw_exports_id => "fwexport.1510768198.5364478" 
) 
location_record.save 

: 나는 다음을 수행하려고하면 콘솔에서 691,363,210

class CreateLocations < ActiveRecord::Migration[5.1] 
    def change 
    create_table :locations, id: :string do |t| 
     t.string :fw_exports_id 
     t.string :city 
     t.string :state 
     t.string :country 
     t.string :api_source 
     t.timestamps 
     t.index [:fw_exports_id], unique: true 
    end 
    end 
end 

,

[9] pry(main)> location_record = Location.new(
[9] pry(main)* :fw_exports_id => "fwexport.1510768198.5364478" 
[9] pry(main)*) 
=> #<Location:0x0000000003e68888 id: nil, fw_exports_id: "fwexport.1510768198.5364478", city: nil, state: nil, country: nil, api_source: nil, created_at: nil, updated_at: nil> 
[10] pry(main)> location_record.save 
    (0.2ms) BEGIN 
    (0.1ms) ROLLBACK 
=> false 

을 내가 할 경우 :

Location.create!(fw_exports_id: "fwexport.1510768198.5364478") 

알겠습니다 :

ActiveRecord::RecordInvalid: Validation failed: Fw export must exist 

어떤 문제일까요?

해결 방법 : 외래 키 필요 fw_export_id에서와 단수 수 있도록

위치, FwExport에 속해 있지 내가 location_record.errors.full_messages 권리를 호출하는 것을 chanve

+1

콘솔에서 'location_record = Location.new (...)'하십시오. 그러면'location_record.valid?'(아마도 false를 반환 할 것입니다). 그런 다음,'location_record.errors.full_messages'. – jvillian

+0

'location_record.valid?'와'location_record.errors'의 결과를 확인 하시겠습니까? 그것은 당신에게 무엇이 잘못되었는지에 대한 단서를 줄 수 있습니다. 또한'save! '를 사용하여 좀 더 자세한 설명적인 오류 메시지를 얻을 수도 있습니다. –

+0

ArgumentError : 잘못된 인수 수 (주어진 0, 예상 2) – EastsideDeveloper

답변

0

시도를했을 때 그것은 일

fw_exports_id 이후 :

location_record = Location.new(:fw_exports_id =>"fwexport.1510768198.5364478") 
location_record.save 

여기에 출력 복사

문제의 원인을 포함 할 수 있습니다.

+0

ArgumentError : 잘못된 인자 수 (주어진 0, 예상 된 2) – EastsideDeveloper

관련 문제