不允许的参数:Rails 5.2

问题描述 投票:0回答:1

我正在构建一个应用程序,用户可以在其中发布不动产属性。因此,我有两个表,一个称为Property,另一个称为Amenity(用于图标,如浴室,游泳池等)。我将Amenity表与Property表分开,因此可以使用它与其他表,我有此错误Unpermitted parameter :: gym

这是我的代码:

property.rb模型

class Property < ApplicationRecord
    belongs_to :owner
    has_many :amenities
    accepts_nested_attributes_for :amenities
end

amenity.rb模型

class Amenity < ApplicationRecord
    belongs_to :property
end

properties_controller.rb

class PropertiesController < ApplicationController
  before_action :set_property, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_owner!

  .
  .
  .

  # POST /properties
  # POST /properties.json
  def create
    @property = current_owner.properties.new(property_params)

    respond_to do |format|
      if @property.save
        format.html { redirect_to @property, notice: 'Tu propiedad ha sido creada!' }
        format.json { render :show, status: :created, location: @property }
      else
        format.html { render :new }
        format.json { render json: @property.errors, status: :unprocessable_entity }
      end
    end
  end

 .
 .
 .

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def property_params
      params.require(:property).permit(:name, :description, :price, amenities_attributes: [:id, :bathroom, :room, :pool, :gym, 
        :kitchen, :terrace, :balcony, :living_room, :garage, :parking_lot, :green_area])
    end
end

便利设施迁移表

    class CreateAmenities < ActiveRecord::Migration[5.2]
      def change
        create_table :amenities do |t|
          t.integer :bathroom
          t.integer :room
          t.integer :pool 
          t.integer :gym
          t.integer :kitchen
          t.integer :terrace
          t.integer :balcony
          t.integer :living_room
          t.integer :garage
          t.integer :parking_lot
          t.integer :green_areas

          t.references :property
          t.timestamps
        end
        add_index :amenities, [:id, :created_at]
      end
    end

属性迁移表

class CreateProperties < ActiveRecord::Migration[5.2]
  def change
    create_table :properties do |t|
      t.string :name
      t.text :description
      t.integer :price
      t.string :services
      t.string :rules
      t.string :address
      t.float :latitude
      t.float :longitude



      t.references :owner
      t.timestamps
    end
    add_index :properties, [:id, :rfc, :created_at]
  end
end

控制台日志

Parameters: {"utf8"=>"✓", "authenticity_token"=>"GjmTFKS3cQRwgrSnTLFOoWQV/gXdTgST0nf7GOs7ZS2i8wneFqzADeTLUo26UKkA5392nrDKGZpVyav4LWpfjw==", "property"=>{"name"=>"Propiedad1", "description"=>"Propiedad1", "price"=>"120000", "gym"=>"1"}, "commit"=>"Create Property"}
  Owner Load (0.3ms)  SELECT  "owners".* FROM "owners" WHERE "owners"."id" = ? ORDER BY "owners"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/kensanchez/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98
Unpermitted parameter: :gym

就我而言,这必须工作正常,但我在理解它时有一些问题。我将感谢您的帮助!谢谢。

ruby-on-rails ruby parameters ruby-on-rails-5
1个回答
0
投票

参数"property"=>{"name"=>"Propiedad1", "description"=>"Propiedad1", "price"=>"120000", "gym"=>"1"}的这一部分应具有与property_params中相同的结构。

参数gym必须在amenities_attributes内部。

例如:"property"=>{"name"=>"Propiedad1", "description"=>"Propiedad1", "price"=>"120000", "amenities_attributes" => { "gym"=>"1" }}

© www.soinside.com 2019 - 2024. All rights reserved.