2016-09-28 81 views
0

紅寶石2.3.0p0(2015年12月25日修訂53290)x86_64的Linux的],Ruby on Rails的沒有的ActiveResource資源節約模式正確

的Rails 4.2.5

我有兩個項目。從第一個項目我通過API獲取數據到第二個項目。 (

class Car < ActiveResource::Base 
    self.site = 'https://myeasyb-vssram.c9users.io' 
    self.format = :json 
end 

Gpstablecontroller第二個項目:

用戶模型中第一個項目:

class Car < ActiveRecord::Base 
    belongs_to :user 
end 

汽車模型(遠程)在第二個項目:在第一個項目

class User < ActiveRecord::Base 
has_many :cars 
end 

汽車模型):

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 attribute.But它正在改變這一切foriegnkeys包括USER_ID車型屬性

使用設計爲1項目的用戶。

回答

0

問題是我給user_id car_params當前user_id。所以我無法通過資源模型進行編輯。所以我改變了這個創建行動。 在我的第一應用程序我有轎廂控制器:

def car_params 
     params.require(:car).permit(:name, :model, :colour, :ac, :gpss, :wifi, :luggage, :cfare, :card, :crfare, :no, :nos, :user_id, {carpicss: []}).**merge(user: :current_user)** 
    end 

我刪除.merge(用戶:CURRENT_USER)從上面的代碼。

,並將此在創建行動

def create 
    @car = Car.new(car_params) 
    @car.card=" #{@car.name} : #{@car.no}" 
    @car.user_id=current_user.id #added here to save current user_id 
    respond_to do |format| 
     if @car.save 
     format.html { redirect_to cars_path, notice: 'Car was successfully created.' } 
     format.json { render :show, status: :created, location: cars_path } 
     else 
     format.html { render :new } 
     format.json { render json: @car.errors, status: :unprocessable_entity } 
     end 
     @car.reload 
    end 
    end