我有以下表单,试图在带有标签所有区域的复选框中加载并允许将其保存到徽标表单页面上的徽标中。
= tb_form_for [:admin, @logo],
remote: true,
data: { errors: :inline, success: admin_logos_path } do |f|
= tb_form_errors(f.object, :base)
= f.tb_text_field :name
= f.tb_check_box :home_page, class: 'checkmark'
= f.tb_file_field :logo_photo
= image_tag @logo.logo_photo, style: 'padding-left: 180px; padding-bottom: 20px;' if @logo.logo_photo.present?
= f.collection_check_boxes :region_id, Region.all, :id, :name, checked: Region.all.map(&:id) do |x|
div
= x.check_box
= x.label
= f.tb_save_buttons('Logo', admin_logos_path)
页面正确加载,但保存时出现错误,我最终遇到
ActionController :: UnpermittedParameters(找到不允许的参数::region_id)
很酷,所以我检查了我的LogosController,并且有以下内容:
class LogosController < ApplicationController
before_action :load_logo, only: [:show, :edit, :update, :destroy]
def create
@logo = Logo.new(logo_params)
flash[:notice] = 'Created' if @logo.save
respond_with @logo
end
def edit
respond_with @logo
end
def update
flash[:notice] = 'Updated' if @logo.update_attributes(logo_params)
respond_with @logo
end
private
def logo_params
params.require(:logo).permit(:name, :home_page, :user, :region, :logo_photo, :region_id, :user_id)
end
end
我对传递的第一个参数尝试了几种变体:
找到了不允许的参数::Region :: ActiveRecord_Associations_CollectionProxy:0x00007f81a79a81e0>
找到了不允许的参数::region
找到了不允许的参数::Logo:0x00007f81a34da8
参数“徽标”的预期数组(Rack :: QueryParser :: Params)
我如何准确地击中这个?
编辑:
从日志中:
Parameters: {"id"=>"12"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.2ms) BEGIN
SQL (0.5ms) UPDATE "users" SET "perishable_token" = $1, "last_request_at" = $2, "updated_at" = $3 WHERE "spud_users"."id" = $4 [["perishable_token", "aFl2KBnsphPP4I6CwXO2"], ["last_request_at", "2018-08-31 15:48:15.999796"], ["updated_at", "2018-08-31 15:48:16.000920"], ["id", 1]]
(5.4ms) COMMIT
Logo Load (0.5ms) SELECT "logos".* FROM "logos" WHERE "logos"."id" = $1 LIMIT $2 [["id", 12], ["LIMIT", 1]]
Rendering admin/logos/edit.html.slim within layouts/admin/detail
Region Load (0.5ms) SELECT "regions".* FROM "regions"
CACHE Region Load (0.0ms) SELECT "regions".* FROM "regions"
Rendered admin/logos/_form.html.slim (111.2ms)
提交表单时登录:
Started PATCH "/admin/logos/12" for 127.0.0.1 at 2018-09-04 09:02:43 -0400
Processing by Admin::LogosController#update as JSON
Parameters: {"utf8"=>"✓", "logo"=>{"name"=>"Mr-T", "home_page"=>"1", "region_id"=>["", "1", "3", "4", "2"]}, "commit"=>"Save Logo", "id"=>"12"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Logo Load (0.2ms) SELECT "logos".* FROM "logos" WHERE "logos"."id" = $1 LIMIT $2 [["id", 12], ["LIMIT", 1]]
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.5ms)
ActionController::UnpermittedParameters (found unpermitted parameter: :region_id):
app/controllers/admin/logos_controller.rb:48:in `logo_params'
app/controllers/admin/logos_controller.rb:32:in `update'
答案 0 :(得分:0)
在“ logo_params”上将“:region”更改为“:region_id”
params.require(:logo).permit(:name, :home_page, :user, :region_id, :logo_photo,:user_id)