使用file_validators gem unpermitted参数进行File_size验证

时间:2015-11-05 03:45:37

标签: ruby-on-rails ruby ruby-on-rails-4

我将file_validators gem添加到我的应用程序中,并在我的vehicle_image.rb模型中调用了验证,您可以在下面看到。

尝试上传新图像后,我在Rails控制台中收到一条未经许可的参数消息。我怀疑错误与强参数有关?我试图在if @ vehicle.save之前分配图像但是不成功。

修改:vehicle_image.rb

class VehicleImage < ActiveRecord::Base
  belongs_to :vehicle
  validates :image, file_size: { less_than_or_equal_to: 500.kilobytes, message: "Image must be less that 500kbs" }
  mount_uploader :image, ImageUploader

  def set_to_primary_and_save
    VehicleImage.where(vehicle: vehicle).update_all(primary: false)
    self.primary = true
    save
  end

end

堆栈跟踪

    Started PATCH "/vehicles/65" for 127.0.0.1 at 2015-11-05 14:03:06 -0500
Processing by VehiclesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"o6W0JsKzxGe9D1z6VA2WeXW3b4JBVsfvYDvM4ANf4Eo5wVFBn1e31y+oKdLIsWFy41WXeW1BUenCzKTE6tni1Q==", "vehicle"=>{"make"=>"Pontiac", "model"=>"GTO", "year"=>"1967", "production_date"=>"January 5, 1968", "engine"=>"454 ", "transmission"=>"4 Speed Muncie", "trim"=>"Red", "color"=>"Black", "options"=>"Tinted Glass, Hurst Shifter", "location"=>"Milton, Ontario", "description"=>"sdfsdfdsf", "vehicle_images"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x007f4adfa1c738 @tempfile=#<Tempfile:/tmp/RackMultipart20151105-7060-d0j694.jpg>, @original_filename="switzerland-3840x2160-alps-mountauns-stars-night-5713.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"vehicle[vehicle_images][image][]\"; filename=\"switzerland-3840x2160-alps-mountauns-stars-night-5713.jpg\"\r\nContent-Type: image/jpeg\r\n">], "image_cache"=>""}}, "commit"=>"Save", "id"=>"65"}
  Vehicle Load (0.1ms)  SELECT  "vehicles".* FROM "vehicles" WHERE "vehicles"."id" = ?  ORDER BY created_at DESC LIMIT 1  [["id", 65]]
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 6]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 6]]
Unpermitted parameter: vehicle_images
   (0.0ms)  begin transaction
   (0.0ms)  commit transaction
   (0.1ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "vehicle_images" ("image", "vehicle_id", "primary", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["image", "switzerland-3840x2160-alps-mountauns-stars-night-5713.jpg"], ["vehicle_id", 65], ["primary", "t"], ["created_at", "2015-11-05 19:03:06.685379"], ["updated_at", "2015-11-05 19:03:06.685379"]]
   (18.5ms)  commit transaction
Redirected to http://localhost:3000/vehicles/65
Completed 302 Found in 2474ms (ActiveRecord: 19.4ms)

vehicles_controller.rb

class VehiclesController < ApplicationController
  def index
    scope = Vehicle.approved
    scope = scope.filter_by_make(params[:makes]) if params[:makes].present?
    scope = scope.filter_by_year(params[:years]) if params[:years].present?

    @vehicles = scope
    authorize @vehicles
  end

  def show
    @vehicle = Vehicle.find(params[:id])
    @primary_image, @images = @vehicle.primary_and_all_vehicle_images
  end

  def new
    @vehicle = Vehicle.new
    authorize @vehicle
  end

  def create
    @vehicle = Vehicle.new(vehicle_params)
    @vehicle.user = current_user
    authorize @vehicle

    if @vehicle.save
      add_vehicle_images if params[:vehicle][:vehicle_images][:image]
      create_registry_request(@vehicle)

      flash[:notice] = "The Vehicle was sent to the Administrator for Approval. You will be notified in your Dashboard if your vehicle was approved or denied."
      redirect_to current_user
    else
      flash[:error] = "There was an error saving the Vehicle to the Registry. Please try again."
      render :new
    end
  end

  def edit
    @vehicle = Vehicle.find(params[:id])
    authorize @vehicle
    @primary_image, @images = @vehicle.primary_and_all_vehicle_images
  end

  def update
    @vehicle = Vehicle.find(params[:id])
    authorize @vehicle

    if @vehicle.update_attributes(vehicle_params)
      add_vehicle_images if params[:vehicle][:vehicle_images][:image]

      flash[:notice] = "The Vehicle entry was updated."
      redirect_to @vehicle
    else
      flash[:error] = "There was an error updating the Vehicle. Please try again."
      @primary_image, @images = @vehicle.primary_and_all_vehicle_images
      render :edit
    end
  end

  def re_edit
    @vehicle = Vehicle.find(params[:id])
    authorize @vehicle
    @primary_image, @images = @vehicle.primary_and_all_vehicle_images
  end

  def resubmit
    #update and new request
    @vehicle = Vehicle.find(params[:id])
    authorize @vehicle

    if @vehicle.update_attributes(vehicle_params)
      add_vehicle_images if params[:vehicle][:vehicle_images][:image]

      Vehicle.transaction do
        @vehicle.active_registry_request.archive
        create_registry_request(@vehicle)
      end

      flash[:notice] = "The Vehicle entry was updated and sent to the Administrator. Please wait for Approval."
      redirect_to @vehicle
    else
      flash[:error] = "There was an error updating the Vehicle. Please try again."
      @primary_image, @images = @vehicle.primary_and_all_vehicle_images
      render :re_edit
    end
  end

  private

  def vehicle_params
    params.require(:vehicle).permit(:make, :model, :year, :production_date, :engine, :transmission, :trim, :color, :options, :location, :description, vehicle_images_attributes: [:image])
  end

  def add_vehicle_images
    params[:vehicle][:vehicle_images][:image].each_with_index do |img, i|
      image = @vehicle.vehicle_images.build(image: img)
      image.primary = true if i == 0
      image.save!
    end
  end

  def create_registry_request(vehicle)
    RegistryRequest.create!(vehicle: vehicle)
  end

end

1 个答案:

答案 0 :(得分:0)

  Parameters: {
    # ... snip ...
    "vehicle" => { # ... snip ... 
      "vehicle_images"=>{ # ... snip ... }
    }       
  }

但参数白名单指定为:

params.require(:vehicle).permit(..., vehicle_images_attributes: [:image])

“vehicle_images”不等于“vehicle_images_attributes”,因此消息:

Unpermitted parameter: :vehicle_images

表单或白名单需要更改,以便params哈希中的键与permit中的参数匹配。

通常情况下,_attributes后缀会在我们使用accepts_nested_attributes_for时添加到表单中,但您似乎没有这样做。