编辑/更新来自activerecords" unpermited params"

时间:2016-08-28 10:06:49

标签: ruby-on-rails activerecord devise cancan ruby-on-rails-5

我有一个应用程序,可以允许proprietaire(用户名)编辑他们的露营(邮件的nome)。帖子有proprietaire_id。这部分有效。

我在另一个名称caracteristiquetest中添加了关于露营(鱼,烧烤)的一些细节。我和camping_id建立了关系。我可以显示鱼和烧烤的价值。但是,当我试图允许proprietaire(用户)编辑/更新他们的露营时,记录不保存,我有一个错误未经合成的参数。

我该怎么做?

对不起我的英语法语

camping_controller.rb

bundle config github.https true

camping.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 

caracteristiquetest.rb

belongs_to :proprietaire
belongs_to :caracteristiquetest
accepts_nested_attributes_for :caracteristiquetest

_form.html.erb

class Caracteristiquetest < ApplicationRecord
  has_one :camping
  accepts_nested_attributes_for :camping
end

修改 development.log

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

EDIT2

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

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

  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)

但我一直都是不允许的参数:piscine

development.log

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

我该怎么办?我失去了......

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

首先感谢您的所有评论,帮助。 我找到了你的解决方案

首先在控制器中

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 %>

现在,工作proprietaire可以更新。但我有另一个问题:)

我尝试了参数#34;未知&#34;,&#34;是&#34;,&#34;否&#34;。例如,我编辑选择&#34;是&#34;,我看到&#34;是&#34;更新后。那么好吧。 但是当我重新编辑时,我会看到第一个选择&#34; Unknown&#34;而不是当前的选择&#34;是&#34;在编辑。解决这个问题的解决方案是什么?