Rails插入嵌套对象

时间:2013-12-03 04:36:33

标签: ruby-on-rails ruby json rest controllers

我们正试图将一个对象插入我们的rails控制器(术语可能是错误的,我是ruby和rails的新手),其中包含包含对象的对象。对于我们试图插入具有餐厅的位置的基本部分,因此餐厅具有指向该位置的id。理想情况下,我们希望它只插入位置(如果它不存在)并在插入后赋予它id,如果它已经存在则只填写已存在的id。现在我们正在插入位置,但餐馆插入永远不会发生。

下面是我们尝试插入的示例json对象,它还有我们尝试插入的其他控制器。位置有一个餐厅,有许多检查,有很多违规行为。表格(地点,餐馆,检查,违规行为)

{
  "locations": {
    "restaurants_attributes": {
      "name": "CORNERSTONE GROUP HOME",
      "type": "INSTITUTION",
      "inspections_attributes": {
        "date": "2013-11-19",
        "number": "26134",
        "violations_attributes": {
          "comment": "WOODEN SHELVING IN DISREPAIR",
          "code": "4-501.11",
          "critical": "0"
        }
      }
    },
    "st_apt_num": "",
    "st_dir": "E",
    "st_number": "1250",
    "st_suffix": "RD",
    "zip_code": "65201",
    "latitude": 38.9808446,
    "longitude": -92.289225
  }
}

以下是地点/餐厅的模型

class Location < ActiveRecord::Base
    has_one :restaurants
    accepts_nested_attributes_for :restaurants
end


class Restaurant < ActiveRecord::Base
        belongs_to :locations
        has_many :inspections
        accepts_nested_attributes_for :inspections
end

以下是位置/餐馆的迁移

class CreateLocations < ActiveRecord::Migration
  def change
    create_table :locations do |t|
      t.integer :st_apt_num
      t.string :st_dir
      t.integer :st_number
      t.string :st_suffix
      t.integer :zip_code
      t.decimal :latitude, :precision => 9, :scale => 7
      t.decimal :longitude, :precision => 9, :scale => 7

      t.timestamps
    end
  end
end


class CreateRestaurants < ActiveRecord::Migration
  def change
    create_table :restaurants do |t|
      t.belongs_to :locations
      t.string :name
      t.string :type

      t.timestamps
    end
  end
end

以下是位置/餐厅的控制器(虽然这里没有改变任何内容)

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

  # GET /locations
  # GET /locations.json
  def index
    @locations = Location.all
  end

  # GET /locations/1
  # GET /locations/1.json
  def show
  end

  # GET /locations/new
  def new
    @location = Location.new
  end

  # GET /locations/1/edit
  def edit
  end

  # POST /locations
  # POST /locations.json
  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 action: 'show', status: :created, location: @location }
      else
        format.html { render action: 'new' }
        format.json { render json: @location.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /locations/1
  # PATCH/PUT /locations/1.json
  def update
    respond_to do |format|
      if @location.update(location_params)
        format.html { redirect_to @location, notice: 'Location was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @location.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /locations/1
  # DELETE /locations/1.json
  def destroy
    @location.destroy
    respond_to do |format|
      format.html { redirect_to locations_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_location
      @location = Location.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def location_params
      params.require(:locations).permit(:st_apt_num, :st_dir, :st_number, :st_suffix, :zip_code, :latitude, :longitude)
    end
end

class RestaurantsController < ApplicationController
  skip_before_action :verify_authenticity_token
  before_action :set_restaurant, only: [:show, :edit, :update, :destroy]

  # GET /restaurants
  # GET /restaurants.json
  def index
    @restaurants = Restaurant.all
  end

  # GET /restaurants/1
  # GET /restaurants/1.json
  def show
  end

  # GET /restaurants/new
  def new
    @restaurant = Restaurant.new
  end

  # GET /restaurants/1/edit
  def edit
  end

  # POST /restaurants
  # POST /restaurants.json
  def create
    @restaurant = Restaurant.new(restaurant_params)

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

  # PATCH/PUT /restaurants/1
  # PATCH/PUT /restaurants/1.json
  def update
    respond_to do |format|
      if @restaurant.update(restaurant_params)
        format.html { redirect_to @restaurant, notice: 'Restaurant was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @restaurant.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /restaurants/1
  # DELETE /restaurants/1.json
  def destroy
    @restaurant.destroy
    respond_to do |format|
      format.html { redirect_to restaurants_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_restaurant
      @restaurant = Restaurant.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def restaurant_params
      params.require(:restaurant).permit(:name, :loc_id, :type)
    end
end

0 个答案:

没有答案