我正在尝试利用remote: true
来允许用户更新布尔值。以下内容可用于“选中”该框(将值更新为true),但在尝试“取消选中”该框(将值更新为false)时不起作用。
<%= check_box_tag("review[selected]", !review[:selected], review[:selected], data: { remote: true, url: review_path(review.id), method: :patch } ) %>
如果未选中该框,而我选择了该框,则会看到以下日志:
Processing by ReviewsController#update as JS
Parameters: {"review"=>{"selected"=>"true"}, "id"=>"89"}
Review Load (0.3ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = $1 LIMIT $2 [["id", 89], ["LIMIT", 1]]
↳ app/controllers/reviews_controller.rb:26
(0.1ms) BEGIN
↳ app/controllers/reviews_controller.rb:28
Location Load (0.2ms) SELECT "locations".* FROM "locations" WHERE "locations"."id" = $1 LIMIT $2 [["id", 8], ["LIMIT", 1]]
↳ app/controllers/reviews_controller.rb:28
Review Update (0.8ms) UPDATE "reviews" SET "selected" = $1, "updated_at" = $2 WHERE "reviews"."id" = $3 [["selected", true], ["updated_at", "2018-08-17 16:42:00.055272"], ["id", 89]]
↳ app/controllers/reviews_controller.rb:28
(0.6ms) COMMIT
↳ app/controllers/reviews_controller.rb:28
Redirected to http://localhost:3001/reviews
但是,当我尝试取消选择该框(将其更新为false)时,我看到:
Processing by ReviewsController#update as JS
Parameters: {"id"=>"89"}
Review Load (0.3ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = $1 LIMIT $2 [["id", 89], ["LIMIT", 1]]
↳ app/controllers/reviews_controller.rb:26
Completed 400 Bad Request in 3ms (ActiveRecord: 0.3ms)
ActionController::ParameterMissing (param is missing or the value is empty: review):
app/controllers/reviews_controller.rb:57:in `review_params'
app/controllers/reviews_controller.rb:28:in `update'
似乎参数没有通过?有没有办法解决这个内联问题?