Anyone Recommend a File Uploader

attachment_fu is nice. http://svn.techno-weenie.net/projects/plugins/attachment_fu/README

* MaD <mayer.dominik@gmail.com> [2009-01-07 06:17:27 -0800]:

attachment_fu is nice. http://svn.techno-weenie.net/projects/plugins/attachment_fu/README

I guess here's the latest version:

I guess here's the latest version:GitHub - technoweenie/attachment_fu: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.

oh, i'm sorry. i still got some old bookmarks on svn instead of github.

There are two parts of the upload form that differ from typical usage.

  1. Include ‘:multipart => true’ in the html options of the form_for tag.

Example:

<% form_for(:attachment_metadata, :url => { :action => “create” }, :html => { :multipart => true }) do |form| %>

  1. Use the file_field helper with :uploaded_data as the field name.

Example:

<%= form.file_field :uploaded_data %>

I suspect you didn’t include the multipart option.

Best regards

Peter De Berdt

  <p>     <%= f.label :description %><br />     <%= f.text_field :description %>   </p>   <p>     <%= f.label :date_due %><br />     <%= f.calendar_date_select :date_due %>   </p>     <p>       <%= f.file_field :uploaded_data %>     </p>   <p>     <%= f.submit "Create" %>   </p> <% end %>

i forgot to state, that i already had the multipart code in there. so i dont thin the problem is that

ProjectFile seems more like an ActiveRecord class name.

Best regards

Peter De Berdt

script/generate model ProjectFile

class ProjectFile < ActiveRecord::Base

No underscores in model names, that’s not the convention.

Best regards

Peter De Berdt

Also, I do hope you have a has_attachment statement in your ProjectFile model?

Best regards

Peter De Berdt

@project_file = Project_File.new(params[:uploaded_data])

The solution is even more obvious:

ProjectFile.new(params[:project][:uploaded_data]

new project view <% form_for(@project, :html => {:multipart => true}) do |f| %> <%= f.file_field :uploaded_data %> <% end %>

And to be honest, this is not really good code either.

You are manually assigning related record ids instead of relying on Rails’ built-in safeguards.

I’d advise u to start looking at railscasts.com, starting for very early ones (complex forms might be a good one for you), getting a Rails book (The Rails Way, Agile Web Development with Rails) and getting to know the framework better.

Best regards

Peter De Berdt

And to be honest, this is not really good code either.

You are manually assigning related record ids instead of relying on Rails' built-in safeguards.

I'd advise u to start looking at railscasts.com, starting for very early ones (complex forms might be a good one for you), getting a Rails book (The Rails Way, Agile Web Development with Rails) and getting to know the framework better.

Best regards

Peter De Berdt

Hi Peter,

Many thanks for your extensive help on this one. I now have

    @project_file = ProjectFile.new(params[:project][:uploaded_data])

in my projects controller.

      <%= f.file_field :uploaded_data %>

but it still says

ActiveRecord::UnknownAttributeError in ProjectsController#create

unknown attribute: uploaded_data

Yes it will. The problem is this: your new project file object expects the attribute :uploaded_data to be present in the hash you are passing it.

@project_file = ProjectFile.new(params[:project])

This will pass in a hash that’s too large (since it also holds your project related attributes, but it will probably save the object for you. That’s the reason you need to get familiar with fat models, skinny controllers. It will make mass assignments and related object creation so much easier.

The only code that should be in your controller is that to assign the instance variable with a newly created object. I would then let my model handle the object creation, including the related record. In fact, that is the exact thing the model is there for.

def create

@project = Project.create(params[:project])

end

Best regards

Peter De Berdt

Responding to the subject line: Please register another vote for Paperclip; it rules!