代码之家  ›  专栏  ›  技术社区  ›  SausageBuscuit

Ruby on Rails回形针协会下拉列表

  •  1
  • SausageBuscuit  · 技术社区  · 10 年前

    我在RoR网站上遇到了Paperclip的问题。我有一个表单中的关联控件,Paperclip中的最大大小设置为1MB。当用户超过此限制时,它将导致关联框中只有“是”和“否”作为选项,如果用户使用新的较小文件提交页面,则会导致严重错误。我看到过超过大小时的错误报告,但不是这个特定的错误。只要用户在限制范围内,图像上传就可以正常工作。

    模型

    class Vehicle < ActiveRecord::Base
      belongs_to :vehicle_location
      has_many :reservations
      has_attached_file :image, styles: { medium: "320x240>", thumb: "32x24>"}
      validates_attachment :image,
                             content_type: { content_type: ['image/jpeg',
                            'image/jpg', 'image/png', 'image/gif'] },
                             size: { less_than: 1.megabytes }
    end
    
    class VehicleLocation < ActiveRecord::Base
      has_many :vehicles
    end
    

    看法

    <%= f.association :vehicle_location, label: false, :collection =>
      @vehicle_locations, :prompt => "Select Location", :wrapper_html => 
      { :style => 'display: inline' } %>
    
    <%= f.file_field :image, label: "Upload an image" %> 
    

    车辆参数值

    "vehicle"=>{"unit_number"=>"TEST123", "car_year"=>"2015", "car_make"=>"Ford", "car_model"=>"Test", "vin_number"=>"123456123456", "average_mileage"=>"", "odometer"=>"0", "next_service"=>"3000", "vehicle_location_id"=>"1", "image"=>#ActionDispatch::Http::UploadedFile:0x46f2480 @original_filename="test.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"vehicle[image]\"; filename=\"test.JPG\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#File:C:/Users/Test~1/AppData/Local/Temp/RackMultipart20150921-460-8hrte0>>, "is_serviced"=>"0", "is_archived"=>"0"}
    

    我也尝试过使用collection_select,但当我这样做时,会出现错误

    ActiveRecord::AssociationTypeMismatch in VehiclesController#create
    
    VehicleLocation(#47412924) expected, got String(#19395840).
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   SausageBuscuit    10 年前

    @控制器的create或update方法中未定义vehicle_locations(在new/edit中)。它只给人一种印象,这是一个回形针问题,因为回形针拒绝是唯一会引发它的事情。