Edit/update 来自活动记录 "unpermited params"

Edit/update from activerecords "unpermited params"

我有一个应用程序可以让业主(用户名)编辑他们的露营地(post 的名字)。 Post 有 proprietaire_id。这部分有效。

我在另一个 table name caracteristiquetest 中添加了一些关于露营(钓鱼、烧烤)的细节。我和 camping_id 建立了关系。能发挥鱼以及烤肉的价值。但是当我尝试允许 proprietaire(user) edit/update 他们的露营时,记录不保存并且我有一个错误 unpermited params.

我该怎么做?

对不起我的英语我是法语

camping_controller.rb

    def update
        respond_to do |format|
          if @camping.update(camping_params)
            format.html { redirect_to @camping, notice: 'Camping was successfully updated.' }
            format.json { render :show, status: :ok, location: @camping }
          else
            format.html { render :edit }
            format.json { render json: @camping.errors, status: :unprocessable_entity }
          end
        end
      end
  def camping_params
      params.require(:camping).permit(:name, :adresse, :code_postale, :commune, :courriel, :site_internet, :tel, :description, :nomdep, :nomregion, :numdep, :slug, :ville_id, :region_id, :departement_id, :latitude, :longitude, :etoile, :user_id, :image)
      params.require(:caracteristiquetest).permit(:piscine, :barbecue)
    end 

camping.rb

belongs_to :proprietaire
belongs_to :caracteristiquetest
accepts_nested_attributes_for :caracteristiquetest

caracteristiquetest.rb

class Caracteristiquetest < ApplicationRecord
  has_one :camping
  accepts_nested_attributes_for :camping
end

_form.html.erb

<%= f.select(:piscine, [['Non renseigné',"Non renseigné"], ['Oui',"Oui"], ['Non',"Non"]], {:selected => (@camping.caracteristiquetest.piscine?)}, {class:"form-control", id:"exampleSelect1"}) %>

编辑 development.log

  Started GET "/campings/1" for 127.0.0.1 at 2016-08-28 14:06:56 +0200
Processing by CampingsController#show as HTML
  Parameters: {"id"=>"1"}
  [1m[36mCamping Load (1719.1ms)[0m  [1m[34mSELECT  "campings".* FROM "campings" WHERE "campings"."id" = ? LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1m[34mSELECT  "campings".* FROM "campings" WHERE "campings"."id" = ? LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
  Rendering campings/show.html.erb within layouts/application
  [1m[36mCaracteristiquetest Load (0.0ms)[0m  [1m[34mSELECT  "caracteristiquetests".* FROM "caracteristiquetests" WHERE "caracteristiquetests"."id" = ? LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
  [1m[36mProprietaire Load (0.0ms)[0m  [1m[34mSELECT  "proprietaires".* FROM "proprietaires" WHERE "proprietaires"."id" = ? LIMIT ?[0m  [["id", 7], ["LIMIT", 1]]
  [1m[36mCamping Load (1900.1ms)[0m  [1m[34mSELECT campings.*, (69.09332411348201 * ABS(campings.latitude - 48.52566299999999) * 0.7071067811865475) + (59.836573914187355 * ABS(campings.longitude - 3.664031) * 0.7071067811865475) AS distance, CASE WHEN (campings.latitude >= 48.52566299999999 AND campings.longitude >= 3.664031) THEN  45.0 WHEN (campings.latitude <  48.52566299999999 AND campings.longitude >= 3.664031) THEN 135.0 WHEN (campings.latitude <  48.52566299999999 AND campings.longitude <  3.664031) THEN 225.0 WHEN (campings.latitude >= 48.52566299999999 AND campings.longitude <  3.664031) THEN 315.0 END AS bearing FROM "campings" WHERE (campings.latitude BETWEEN 48.38093121688914 AND 48.67039478311084 AND campings.longitude BETWEEN 3.445496852835473 AND 3.8825651471645273 AND campings.id != 1) ORDER BY distance ASC[0m
  [1m[36mProprietaire Load (1.0ms)[0m  [1m[34mSELECT  "proprietaires".* FROM "proprietaires" WHERE "proprietaires"."id" = ? ORDER BY "proprietaires"."id" ASC LIMIT ?[0m  [["id", 7], ["LIMIT", 1]]
  Rendered campings/show.html.erb within layouts/application (2184.1ms)
  Rendered campings/index.html.erb within layouts/application (19341.1ms)
  Rendered devise/menu/_registration_items.html.erb (1.0ms)
  Rendered devise/menu/_login_items.html.erb (1.0ms)
  Rendered layouts/partials/_nav.html.erb (100.0ms)
  Rendered layouts/partials/_messages.html.erb (1.0ms)
  [1m[36mProprietaire Load (0.0ms)[0m  [1m[34mSELECT  "proprietaires".* FROM "proprietaires" WHERE "proprietaires"."id" = ? ORDER BY "proprietaires"."id" ASC LIMIT ?[0m  [["id", 7], ["LIMIT", 1]]
  Rendered layouts/partials/_footer.html.erb (1.0ms)
  Rendered devise/menu/_registration_items.html.erb (5.0ms)
Completed 200 OK in 8799ms (Views: 1524.3ms | ActiveRecord: 3620.2ms)


  Rendered devise/menu/_login_items.html.erb (0.0ms)
  Rendered layouts/partials/_nav.html.erb (75.0ms)
  Rendered layouts/partials/_messages.html.erb (1.0ms)
  Rendered layouts/partials/_footer.html.erb (1.0ms)
Completed 200 OK in 20182ms (Views: 19855.4ms | ActiveRecord: 2.0ms)


Started GET "/campings/1/edit" for 127.0.0.1 at 2016-08-28 14:07:08 +0200
Processing by CampingsController#edit as HTML
  Parameters: {"id"=>"1"}
  [1m[36mCamping Load (0.0ms)[0m  [1m[34mSELECT  "campings".* FROM "campings" WHERE "campings"."id" = ? LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
  [1m[36mProprietaire Load (0.0ms)[0m  [1m[34mSELECT  "proprietaires".* FROM "proprietaires" WHERE "proprietaires"."id" = ? ORDER BY "proprietaires"."id" ASC LIMIT ?[0m  [["id", 7], ["LIMIT", 1]]
  Rendering campings/edit.html.erb within layouts/application
  [1m[36mCaracteristiquetest Load (1.0ms)[0m  [1m[34mSELECT  "caracteristiquetests".* FROM "caracteristiquetests" WHERE "caracteristiquetests"."id" = ? LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
  Rendered campings/_form.html.erb (12.0ms)
  Rendered campings/edit.html.erb within layouts/application (61.0ms)
  Rendered devise/menu/_registration_items.html.erb (1.0ms)
  Rendered devise/menu/_login_items.html.erb (1.0ms)
  Rendered layouts/partials/_nav.html.erb (51.0ms)
  Rendered layouts/partials/_messages.html.erb (0.0ms)
  Rendered layouts/partials/_footer.html.erb (1.0ms)
Completed 200 OK in 487ms (Views: 463.3ms | ActiveRecord: 1.0ms) 
  Started PATCH "/campings/1" for 127.0.0.1 at 2016-08-28 14:07:18 +0200
Processing by CampingsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"1H6bsZ6d5Z8HVa0X8hUp0LQUg+SdU6CmqUEIrP7eSip0f3yVnzKlaPojwYz+AGS9a4wz4LtVeZU10G70TEIt2g==", "camping"=>{"name"=>"DOMAINE DE LA NOUE DES ROIS", "adresse"=>"rue machin", "code_postale"=>"10100", "commune"=>"ma commune", "courriel"=>"email@email.com", "site_internet"=>"www.exemple.com", "tel"=>"0101010101", "description"=>"ma description", "etoile"=>"3", "piscine"=>"Non"}, "commit"=>"Enregistrer", "id"=>"1"}
  [1m[36mCamping Load (0.0ms)[0m  [1m[34mSELECT  "campings".* FROM "campings" WHERE "campings"."id" = ? LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
  [1m[36mProprietaire Load (1.0ms)[0m  [1m[34mSELECT  "proprietaires".* FROM "proprietaires" WHERE "proprietaires"."id" = ? ORDER BY "proprietaires"."id" ASC LIMIT ?[0m  [["id", 7], ["LIMIT", 1]]
Unpermitted parameter: piscine
Completed 400 Bad Request in 5ms (ActiveRecord: 1.0ms)



ActionController::ParameterMissing (param is missing or the value is empty: caracteristiquetest):

app/controllers/campings_controller.rb:111:in `camping_params'
app/controllers/campings_controller.rb:82:in `block in update'
app/controllers/campings_controller.rb:81:in `update'
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (6.0ms)
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.0ms)
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (1753.1ms)

EDIT2

我在 camping.rb 上添加了 attr_accessor :piscine 但我有同样的错误


EDIT3 我在我的控制器上添加了这个

(... :image, caracteristiquetest_attributes: [:piscine, :barbecue])

但我总是总是不允许参数:piscine

development.log

  Started PATCH "/campings/1" for 127.0.0.1 at 2016-08-28 17:24:27 +0200
    Processing by CampingsController#update as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"I7otqLV4d8oqWdCncp5yeZK1tX/NeijUMHpA+XtH+zGDu8qMtNc3PdcvvDx+iz8UTS0Fe+t88ees6yahyducwQ==", "camping"=>{"name"=>"nomducamp", "adresse"=>"adresse1", "code_postale"=>"10100", "commune"=>"SAINT-HILAIRE-SOUS-ROMILLY", "courriel"=>"mail@mail.com", "site_internet"=>"www.example.com", "tel"=>"0101010101", "description"=>"desc", "etoile"=>"3", "piscine"=>"Non renseigné"}, "commit"=>"Enregistrer", "id"=>"1"}
      [1m[36mCamping Load (0.0ms)[0m  [1m[34mSELECT  "campings".* FROM "campings" WHERE "campings"."id" = ? LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
      [1m[36mProprietaire Load (0.0ms)[0m  [1m[34mSELECT  "proprietaires".* FROM "proprietaires" WHERE "proprietaires"."id" = ? ORDER BY "proprietaires"."id" ASC LIMIT ?[0m  [["id", 7], ["LIMIT", 1]]
    Unpermitted parameter: piscine
      [1m[35m (0.0ms)[0m  [1m[36mbegin transaction[0m
      [1m[35m (1.0ms)[0m  [1m[36mcommit transaction[0m
    Redirected to http://localhost:3000/campings/1
    Completed 302 Found in 579ms (ActiveRecord: 1.0ms)

我能做什么?我迷路了...

要传递嵌套属性(例如,另一个模型的属性),您可以在同一许可语句中将其作为散列传递:

(... :图片, caracteristiquetest_attributes: [:鱼, :烧烤])

首先感谢大家的评论,帮助。 我和你一起找到解决方案

首先在控制器中

caracteristiquetest_attributes: [:piscine, :barbecue]

形式

<%= f.fields_for :caracteristiquetest do |builder| %>
<%= builder.select(:piscine, [['Non renseigné',"Non renseigné"], ['Oui',"Oui"], ['Non',"Non"]], {:selected => (@camping.caracteristiquetest.piscine?)}, {class:"form-control", id:"exampleSelect1"}) %>
  <% end %>

现在那是专有的作品可以更新了。但我还有一个问题:)

我尝试了参数 "Unkown"、"Yes"、"No"。例如我编辑选择 "Yes",我在更新后看到 "Yes"。那么好吧。 但是当我重新编辑时,我在编辑器中看到了第一个选择 "Unknown" 而不是当前选择 "Yes"。解决此问题的解决方案是什么?