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
The model looks like:
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
The view form:
<%= 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 %>
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
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?