簡體   English   中英

不允許的參數:Rails 5.2

[英]Unpermitted parameter: Rails 5.2

我正在構建一個應用程序,用戶可以在其中發布房地產屬性。 所以我有兩個表,一個叫做Property ,另一個叫做Amenity (用於浴室、游泳池等圖標)我將 Amenity 表與 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

就我而言,這必須正常工作,但我在理解它時遇到了一些問題。 我將感謝您的幫助! 謝謝。

編輯:

我的網頁表格

<%= form_with(model: property, local: true) do |form| %>
    <% if property.errors.any? %>
        <div id="error_explanation">
            <h2><%= pluralize(property.errors.count, "error") %> prohibited this property from being saved:</h2>

            <ul>
                <% property.errors.full_messages.each do |message| %>
                    <li><%= message %></li>
                <% end %>
            </ul>
        </div>
    <% end %>

    <div class="container">
        <div class="field">
            <%= form.label :name %>
            <%= form.text_field :name %>
        </div>

        .
        .
        .
       <!--Gym attribute from amenities-->
        <div class="field">
            <%= form.label :gym %>
            <%= form.number_field :gym %>
        </div>

        <div class="actions">
            <%= form.submit %>
        </div>
    </div>
<% end %>

這部分參數"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" }]}

UPD

看看這個https://guides.rubyonrails.org/form_helpers.html#nested-forms

嘗試在表單視圖中使用這段代碼:

<!--Gym attribute from amenities-->
<%= form.fields_for :amenities do |amenities_form| %>
  <div class="field">
    <%= amenities_form.label :gym %>
    <%= amenities_form.number_field :gym %>
  </div>
<% end %>

這是我在您的控制台日志輸出中看到的

"property"=>{"name"=>"Propiedad1", "description"=>"Propiedad1", "price"=>"120000", "gym"=>"1"}

這些是屬性的參數,最后一個值是"gym"=>"1" ,這就是你得到unpermitted parameter的原因。

它應該出現在accommodations_attributes下,例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM