分配给上传者时的Rails 4 carrierwave gem TypeError [英] Rails 4 carrierwave gem TypeError on assign to uploader

查看:88
本文介绍了分配给上传者时的Rails 4 carrierwave gem TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用carrierwave gem在嵌套表单上设置图片上传。上传器看起来像:

I'm using the carrierwave gem to set up image upload on a nested form. The uploader looks like:

class CarImageUploader < CarrierWave::Uploader::Base

    include CarrierWave::MiniMagick

    if Rails.env.production?
        storage :fog
    else
        storage :file
    end

    def store_dir
        "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end

    def default_url
        'default-no-car-pic.png'
    end

    version :thumb do
        process :resize_to_fit => [50, 50]
    end

    def extension_white_list
        %w(jpg jpeg gif png)
    end

end

模型如下:

class Car < ActiveRecord::Base

    belongs_to :user
    has_one :car_info, dependent: :destroy
    has_one :car_spec, dependent: :destroy
    accepts_nested_attributes_for :car_spec, :car_info

    mount_uploaders :car_images, CarImageUploader

    validates_associated :car_info, :car_spec, presence: true

end

视图表单:

<%= form_for @car, html: { multipart: true, class: "form-horizontal" } do |f| %>

    <%= f.fields_for :car_spec do |car_spec_field| %>

        # Fields for car_spec

    <% end %>

    <%= f.fields_for :car_info do |car_info_field| %>

        # Fields for car_info

    <% end %>

    <%= f.label :images %>
    <%= f.file_field :images, multiple: true %>

    <%= f.submit "Add car" %>

<% end %>

<$ c $中的 create 操作c> CarsController 看起来像:

The create action in CarsController looks like:

@car = current_user.cars.build(car_params)
@car.car_images = params[:car][:images]

respond_to do |format|
  if @car.save

    format.html { redirect_to @car, notice: 'Car was successfully created.' }
    format.json { render :show, status: :created, location: @car }
  else
    format.html { render :new }
    format.json { render json: @car.errors, status: :unprocessable_entity }
  end
end

此代码在CarsController#create中导致 TypeError / code> 提交表单时无法将Array强制转换为 car_images 字段已作为 json 添加到 cars 表中领域。根据在carrierwave github上的说明,这是正确的,但是在表单提交时抛出了以上错误。是什么导致此错误,我该如何纠正代码以便提交表单?

This code results in TypeError in CarsController#create can't cast Array to when I submit the form. The car_images field was added to the cars table asjson field. According to the instructions on carrierwave github this is correct, but it is throwing the above error on form submit. What is causing this error and how can I correct the code so that the form will submit?

推荐答案

我遇到了相同的错误。

I had the same error.


TypeError:无法将数组转换为

TypeError: can't cast Array to

如文档 https://github.com/carrierwaveuploader/carrierwave#multiple-文件上传您需要进行迁移

rails g migration add_avatars_to_users avatars:json

问题出在类型列 json 中。 SQLite3和MySQL在PostgreSQL中没有这种类型。您可以将其迁移为 text

The problem is in type column json. SQLite3 and MySQL have not this type as it in PostgreSQL. You can migrate it as text

rails g migration add_avatars_to_users avatars:text

对我来说,这就是工作。

For me it's work.

这篇关于分配给上传者时的Rails 4 carrierwave gem TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆