루비 2.3.0p0 (2015년 12월 25일 개정 53290) [x86_64에-리눅스,루비 레일 ActiveResource에없는 자원 모델을 절약 제대로
레일 4.2.5
나는 두 개의 프로젝트가 있습니다. 1 프로젝트에서 나는 API를 통해 두 번째 프로젝트에 데이터를 가져오고 있습니다. 1 프로젝트
사용자 모델 (원격)
class Car < ActiveRecord::Base
belongs_to :user
end
자동차 모델 2 프로젝트 : : 1 프로젝트
class User < ActiveRecord::Base
has_many :cars
end
자동차 모델
class Car < ActiveResource::Base
self.site = 'https://myeasyb-vssram.c9users.io'
self.format = :json
end
Gpstablecontroller (2 차 프로젝트) :
class GpstablesController < ApplicationController
before_action :set_gpstable, only: [:show, :edit, :update, :destroy]
# GET /gpstables
# GET /gpstables.json
def index
@gpstables = Gpstable.all
end
# GET /gpstables/1
# GET /gpstables/1.json
def show
end
# GET /gpstables/new
def new
@gpstable = Gpstable.new
@gpstables = Gpstable.all
end
# GET /gpstables/1/edit
def edit
@gpstables = Gpstable.all
end
# POST /gpstables
# POST /gpstables.json
def create
@cars = Car.all
@gpstable = Gpstable.new(gpstable_params)
@cars.each do |car|
if @gpstable.car_id == car.id
@car = car
end
end
@car.update_attribute(:gpss, @gpstable.device_id)
respond_to do |format|
if @gpstable.save
format.html { redirect_to gpstables_url, notice: 'Gpstable was successfully created.' }
format.json { render :show, status: :created, location: @gpstable }
else
format.html { render :new }
format.json { render json: @gpstable.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /gpstables/1
# PATCH/PUT /gpstables/1.json
def update
respond_to do |format|
if @gpstable.update(gpstable_params)
Car.all.each do |car|
if @gpstable.car_id == car.id.to_json
@car = car
end
if @gpstable.device_id == car.gpss
car.gpss = 0
car.save!
end
end
@car.gpss = @gpstable.device_id
@car.save!
format.html { redirect_to @gpstable, notice: 'Gpstable was successfully updated.' }
format.json { render :show, status: :ok, location: @gpstable }
else
format.html { render :edit }
format.json { render json: @gpstable.errors, status: :unprocessable_entity }
end
end
end
# DELETE /gpstables/1
# DELETE /gpstables/1.json
def destroy
@cars.each do |car|
if @gpstable.device_id == car.gpss
car.gpss = 0
car.user_id = @gpstable.user_id
car.save
end
end
@gpstable.destroy
respond_to do |format|
format.html { redirect_to gpstables_url, notice: 'Gpstable was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_gpstable
@gpstable = Gpstable.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def gpstable_params
params.require(:gpstable).permit(:device_id, :car_id, :user_id)
end
end
gpstable 레코드를 만들 때 Gpss 속성이 자동차 모델 (원격, 호출 API) 인 경우 업데이트하려고합니다.
가 업데이트됩니다 GPSS는 user_id를 포함한 모든 foriegnkeys을 변화자동차 모델의 속성을 널에 attribute.But.
첫 번째 프로젝트의 사용자에게는 devise를 사용합니다.