I have a very simple configuration for uploading files. I just want to upload an image to the database (I know, I know ...) with a description. It.
This is what my model looks like (which I learned from here ). Basically, this is exactly how the example is configured, only using the attr_protected line for Rails 3.2 to work:
Photo < ActiveRecord::Base # note: there is also a :description attribute!! attr_protected :file_name, :content_type, :binary_data def image_file=(input_data) self.file_name input_data.original_filename self.content_type = input_data.content_type.chomp self.binary_data = input_data.read end end
Here's what my very simple view looks like:
<h1>New Photo</h1> <%= form_for(@photo) do |f| %> <fieldset> <legend>Upload a Photo</legend> <div class="control-group"> <label class="control-label">Photo</label> <div class="controls"> <%= f.file_field :image_file %> </div> </div> <div class="control-group"> <%= f.label :description, class: "control-label" %> <div class="controls"> <%= f.text_field :description, class: "text_field" %> </div> </div> <div class="form-actions"> <%= f.submit "Upload", class: "btn btn-primary" %> <%= link_to "Cancel", photos_path, class: "btn" %> </div> </fieldset> <% end %>
And finally (just for completeness), here is my Create action (simple forests):
def create @photo = Photo.new(params[:photo]) respond_to do |format| if @photo.save format.html { redirect_to @photo, notice: 'Photo was successfully created.' } format.json { render json: @photo, status: :created, location: @photo } else format.html { render action: "new" } format.json { render json: @photo.errors, status: :unprocessable_entity } end end end
The idea is that the user selects a file and types a short description, which should be published in the Create method on the /photos controller. Unfortunately, when I click the "Submit" button, I get a very vague error that does not give me information about what the real problem is:
ArgumentError in PhotosController#create wrong number of arguments (1 for 0) Rails.root: /Users/me/dev/photo-app Application Trace | Framework Trace | Full Trace app/models/photo.rb:4:in `image_file=' app/controllers/photos_controller.rb:43:in `new' app/controllers/photos_controller.rb:43:in `create' Request Parameters: {"utf8"=>"✓", "authenticity_token"=>"qSjMHgjCDo2ORmTvnujrljfPCj6oekLI9KAz4x5gA7Q=", "photo"=>{"image_file"=>#<ActionDispatch::Http::UploadedFile:0x00000101726fd0 @original_filename="SomePic.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image_file]\"; filename=\"SomePic.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/kc/8hsmx29j3xn2_j_9tsw8bz6w0000gn/T/RackMultipart20121001-52387-lre7zh>>, "description"=>"my description"}, "commit"=>"Upload"}
I went through all of Google and StackOverflow, but nothing seems like my (very simple) example. Any help would be greatly appreciated!
Thanks in advance!