Reputation: 789
My view
<%= form_tag({:action => 'upload_image', :car_id => @car.id}, :multipart => true) do %>
<label for="upload_file"><%= t('field.select_image') %></label>
<%= file_field 'upload', 'datafile' %> <%= submit_tag t('field.upload_file') %>
<% end %>
My controller
def upload_image
if params[:upload].present? && params[:car_id].present?
DataFile.save_image_file(params[:upload][:datafile], params[:car_id]) # My methods to save image and other operations with file
end
redirect_to images_path(:car_id => params[:car_id])
end
My Rspec Test (doesn't work)
before :each do
@car = FactoryGirl.create(:car)
@file = fixture_file_upload('/files/test-bus-1.jpg', 'image/jpg')
end
it "can upload a car" do
post :upload_image, :upload => @file, :car_id => @car.id
response.should redirect_to images_path(:car_id => @car.id)
end
Error: Failure/Error: post :upload_image, :upload => @file, :car_id => @car.id NoMethodError: undefined method `[]' for # File:/tmp/test-bus-1.jpg20120826-29027-plg28d
What's wrong?
Upvotes: 0
Views: 993
Reputation: 27374
It looks to me, based on how you've set up your form (since you are calling params[:upload][:datafile]
), that you need to change your rspec test to:
post :upload_image, :upload => { :datafile => @file }, :car_id => @car.id
Upvotes: 1