Question

I'm following the Railscast tutorial for Carrierwave uploads, and have the same files set up. An upload works fine if I upload a file via the console, but my form doesn't seem to post correctly.

I've got my basic form, which includes:

<%= f.label :receipt %><br>
<%= f.file_field :receipt %>

Rails 4 does automatically include a enctype="multipart/form-data" tag on the form.

When I submit the form, I take a look at the server logs. The upload shows up in the parameters:

"receipt"=>#<ActionDispatch::Http::UploadedFile:0x007fcb25c624e0 @tempfile=#<Tempfile:/var/folders/ty/yks2xd9n76z9p49qzprvt1900000gn/T/RackMultipart20130903-53712-17zblz4>, 
@original_filename="Avatar.jpg", 
@content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"expense[receipt]\"; filename=\"Avatar.jpg\"\r\nContent-Type: image/jpeg\r\n">},

However, the INSERT statement doesn't include any mention of my receipt field or file. There isn't any error. The file is not uploaded to the directory I specified in Carrierwave either.

Any ideas on what might be going wrong here? This is not a duplicate of this answer which was an issue with jQuery Mobile. I'm really stumped on what's going on.

Edit: I should probably include my model as well:

class Expense < ActiveRecord::Base
    mount_uploader :receipt, ReceiptUploader
end

I have a stock uploader class as well, generated by Carrierwave:

class ReceiptUploader < CarrierWave::Uploader::Base
    storage :file

    def store_dir
        "receipts"
    end
end

Edit 2: Here is my controller for creating an expense:

def new
  @expense = Expense.new
end

def create
  @expense = Expense.new(expense_params)

  respond_to do |format|
    if @expense.save
      format.html { redirect_to @expense, notice: 'Expense was successfully created.' }
      format.json { render action: 'show', status: :created, location: @expense }
    else
      format.html { render action: 'new' }
      format.json { render json: @expense.errors, status: :unprocessable_entity }
    end
  end
end
Was it helpful?

Solution

Looks like the params wasn't permitting the upload to go through. Rookie mistake.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top