不允许的参数:Rails 5.2
Unpermitted parameter: Rails 5.2
我正在构建一个应用程序,用户可以在其中发布房地产。所以我有两个 table,一个叫做 属性,另一个叫做 Amenity(对于像浴室这样的图标,游泳池等)我将便利设施 table 与 属性 table 分开,这样我就可以将它与其他 table 一起使用,但出现此错误 不允许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
便利设施迁移table
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
属性迁移table
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.
的原因
它应该出现在amenities_attributes下面,比如
"property"=>{"name"=>"Propiedad1", "description"=>"Propiedad1", "price"=>"120000"}, "amenities_attributes": [{ "gym"=>"1" }] }
我正在构建一个应用程序,用户可以在其中发布房地产。所以我有两个 table,一个叫做 属性,另一个叫做 Amenity(对于像浴室这样的图标,游泳池等)我将便利设施 table 与 属性 table 分开,这样我就可以将它与其他 table 一起使用,但出现此错误 不允许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
便利设施迁移table
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
属性迁移table
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.
的原因它应该出现在amenities_attributes下面,比如
"property"=>{"name"=>"Propiedad1", "description"=>"Propiedad1", "price"=>"120000"}, "amenities_attributes": [{ "gym"=>"1" }] }