2016-09-28 3 views
0

рубин 2.3.0p0 (2015-12-25 ревизия 53290) [x86_64-линукс],Рубин на Rails ActiveResource не спасаем Ресурсная модель правильно

Rails 4.2.5

У меня есть два проекта. из 1-го проекта я получаю данные во второй проект через api.

модель пользователя в 1-ом проекте:

class User < ActiveRecord::Base 
has_many :cars 
end 

Модель автомобиля в 1-ом проекте:

class Car < ActiveRecord::Base 
    belongs_to :user 
end 

модели автомобиля (дистанционный) в 2 проекта:

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

Gpstablecontroller (второй проект):

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 текущему user_id в car_params. поэтому я не смог редактировать это через модель ресурсов. поэтому я изменил это, чтобы создать действие. В моем первом приложении у меня есть контроллер автомобиля:

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 
Смежные вопросы