1
votes

Thanks in advance to any help everyone. I'm working with a rails 4 app and want to upload multiple image files using CarrierWave. My form works but after submit the only update to the database is a new record with the image field as nil.

Here are the server logs at submit (abridged)

Parameters: "property"=>{"name"=>"Windsong Ranch"}, 
"images"=>["53a2135761af0826.jpg", "53a2137620ecc597.jpg"], "commit"=>"Edit Property Changes", "id"=>"3"}

And when it saves to the database

INSERT INTO "propimages" ("image", "property_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["image", nil], ["property_id", 3], ["created_at", "2015-07-15 16:42:41.607696"], ["updated_at", "2015-07-15 16:42:41.607696"]]

Here is the form(abridged)

<%= form_for @prop do |f| %>
  <p>
    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control', required: true %>
  </p>
 <p>Upload Images<br>
    <%= file_field_tag "images[]", type: :file, :multiple => true %>
  </p>
  <p>
  <%= f.submit "Edit Property Changes", class: 'btn btn-success' %>
  </p>
<% end %>

image_uploader.rb

  class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  storage :fog

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

  process :resize_to_fill => [1000, nil]

  version :thumb do
    process resize_to_fill: [100, nil]
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end


end

propimage.rb

class Propimage < ActiveRecord::Base
  belongs_to :property
  mount_uploader :image, ImageUploader
end

properties_controller.rb

def create
@prop = Property.create(property_params)
  if @prop.save

    if params[:images]
      params[:images].each { |image|
        @prop.propimages.create(image: image)
      }
    end
    redirect_to @prop
  else
    flash[:alert] = "error creating Property"
    render :new
  end
end

def update
  @prop = Property.find(params[:id])
  if @prop.update_attributes(property_params)
    if params[:images]
        params[:images].each { |image|
            @prop.propimages.create(image: image)
        }
    end
    flash[:notice] = "Update successful"
    redirect_to property_path
  else
    flash[:notice] = "Error updating Sponsor"
    render :new
  end
end
  def property_params
    params.require(:property).permit(:name, :purchase_price, :min_investment, :overview, :targeted_irr, :targeted_hold_period, :targeted_yield, :address_street, :address_city, :address_state, :address_zipcode, :sponsor_id)
  end

propimages_controller

  def create
    @propimage = Propimage.new(propimages_params)
    @propimage.save
  end
 private
  def propimages_params
    params.require(:propimage).permit(:image, :property_id)
  end
2
Please post your property_params method. - Pavan
Updated to include the params, thanks - Chris Thompson
Try propimages.create(image: image, property_id: @prop.id) instead of @prop.propimages.create(image: image). If that doesn't work try moving if params[:images] params[:images].each { |image| @prop.propimages.create(image: image) } end above if @prop.save - Pavan
Are you currently updating a form? - Pavan
The same thing happens on update and on new, the images are in the parameters and then they are nil. And nothing gets pushed to S3. Making those changes gave the same result as before, thanks for the suggestions though. - Chris Thompson

2 Answers

1
votes

Unless there's been an ImageMagick update that I haven't seen, which is possible, the 'nil' parameter is not valid in your image_uploader.rb

  process :resize_to_fill => [1000, nil]

  version :thumb do
    process resize_to_fill: [100, nil]
  end

You must provide a height parameter. I don't think you can just default that out by making it nil. It would be easy enough to test.

  process :resize_to_fill => [1000, 1000]

  version :thumb do
    process resize_to_fill: [100, 100]
  end
1
votes

You're checking for the "images" attribute, but you don't permit it to be sent by the request:

params.require(:propimage).permit(:image, :property_id)

So try using:

params.require(:propimage).permit(:image, :images, :property_id)

PS: You should really refactor this code though.