2016-09-28 72 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