Topic: multiple uploads with attachment_fu II

I had a previous post on this which I deleted b/c I actually made some progress..

Single uploads work like so:

<% form_for(:attachment, :url => attachments_path, 
:html => { :multipart => true }) do |f| -%>
        <%= f.file_field :uploaded_data %>
<%= submit_tag 'Create' %>

In order to handle two uploads. I added this code to my Solutions form:

<%= file_field_tag "image[]", :class => "formField" %>
<%= file_field_tag "image[]", :class => "formField" %>

And in my Solutions controller, create action I've got:

params[:image].each do |image|
  @solution.attachments << Attachment.new(:uploaded_data => image)
end unless params[:image].nil?

It *almost* works.. I'm getting an error that says:

undefined method `content_type' for "tune.png":String

So I guess the param is being interpreted as a string instead of an image?  Not sure why attachment_fu doesn't like this.. Anyone have any pointers?

Re: multiple uploads with attachment_fu II

I think what you're getting back in the param is a pointer to a temp file containing the uploaded image.

Try using .read on the param and see if you can get the file data that way.

Re: multiple uploads with attachment_fu II

I'm not so sure that's it..  As an experiment I just put:

@test = params[:image].read

which gave me this error:

undefined method `read' for ["image1.jpg", "image2.png"]:Array

If instead I change my code to be

@solution.attachments << Attachment.new(:uploaded_data => image.read)

The error is

undefined method `read' for "image1.jpg":String

Any other thoughts?

Re: multiple uploads with attachment_fu II

Bah, it turned out to be a simple missing  :multipart => true in my form.  Grrr..

Re: multiple uploads with attachment_fu II

That was my first guess, but I saw it in the example code you posted. Glad you figured it out smile

Re: multiple uploads with attachment_fu II

I am having the same problem. and i have multipart in my form...can someone shead some light on this please...below is the error message

NoMethodError in AdminController#create

undefined method `read' for "Burton and Ali - ATL Carnival.jpg":String


Request

Parameters: {"multipart"=>"true", "commit"=>"Create", "model_x"=>{"gallery_id"=>"5", "title"=>"painting1", "medium"=>"", "description"=>"", "height"=>"", "image_data"=>"Burton and Ali - ATL Carnival.jpg", "support"=>"", "width"=>""}, "model"=>"painting"}

Re: multiple uploads with attachment_fu II

Hey,

Im new to this and have been trying ti implement the same way but get an error saying "You have a nil object when you didn't expect it!
The error occurred while evaluating nil.mugshots"

Code


<-----create.rhtml--->
<%= error_messages_for :mugshot %>

<% form_for(:mugshot, :url => {:controller => 'mugshots', :action => 'create'},
              :html => { :multipart => true }) do |form| -%>
    <p>
        <label for ="mugshot">Upload A Mugshot:</label>
        <%= file_field_tag "image[]", :class => "formField" %>
        <%= file_field_tag "image[]", :class => "formField" %>
    </p>
   
    <p>
        <%= submit_tag 'Create' %>
    </p>
<% end -%>

<----mugshots_controller---->
def create
       
        params[:image].each do |image|
        if image.size > 0
        @mugshot.mugshots << Mugshot.new(:uploaded_data => image)
        end
    end unless params[:image].nil?
    end

Re: multiple uploads with attachment_fu II

@raj seems @mugshots is nil. And no wonder, i don't see any code in the create
method that initiates this object.

@ikiini show some controller & form code

Re: multiple uploads with attachment_fu II

Sorry my bad i added the initiation. But now I get an error saying "undefined method `mugshots' for #<Mugshot:0xb716cb0c>".

<-----mugshots_controller---->
def create
        @mugshot = Mugshot.new
        params[:image].each do |image|
        if image.size > 0
        @mugshot.mugshots << Mugshot.new(:uploaded_data => image)
        end
    end unless params[:image].nil?
    end

<-----create.rhtml----->
<%= error_messages_for :mugshot %>

<% form_for(:mugshot, :url => {:controller => 'mugshots', :action => 'create'},
              :html => { :multipart => true }) do |f| -%>
    <p>
        <label for ="mugshot">Upload A Mugshot:</label>
        <%= file_field_tag "image[]", :class => "formField" %>
    </p>
   
    <p>
        <%= submit_tag 'Create' %>
    </p>
<% end -%>

Re: multiple uploads with attachment_fu II

a Mugshot has_many mugshots? is that a defined association? Doesn't seem so. What kind of a relation is that? is a Mugshot an attachment?

Sorry i don't get in any way what you try to do by adding multiple mugshots created from uploaded pictures to an empty mugshot ?!?

don't you just want to do this?

def create
  #initiate an array
  @mugshots = []
  params[:image].each do |image|
    if image.size > 0
    #create new mugshot objects from images and add them to the array
    @mugshots << Mugshot.create(:uploaded_data => image)
    end
  end unless params[:image].nil?
end

#view
<% @mugshots.each do |mugshot| ........ %>


Your code doesn't make much sense (to me) without some info, sorry.

Last edited by Duplex (2007-08-14 07:37:45)

11

Re: multiple uploads with attachment_fu II

Sorry again as I'm new to this. I'll go over it again thanks for your help.

Re: multiple uploads with attachment_fu II

Didn't mean to sound harsh in case that seemed so. your code was just very confusing wink

We've all been new to this, no need for excuses.

Last edited by Duplex (2007-08-14 08:30:18)

13

Re: multiple uploads with attachment_fu II

No worries mate. I have understood it now. Thanks. Just a quick question. Do you know if there is a feature to upload images in batch rather then individually browsing for images and then uploading them?

Thanks

Re: multiple uploads with attachment_fu II

raj wrote:

No worries mate. I have understood it now. Thanks. Just a quick question. Do you know if there is a feature to upload images in batch rather then individually browsing for images and then uploading them?

Thanks

Try this..
http://www.urbanpuddle.com/articles/200 … achment-fu

15

Re: multiple uploads with attachment_fu II

@ viniosity: Thanks for this, but I have already implemented this one. Was just wondering whether there is another more advanced approach say if you wanted to upload images in a batch say e.g. hundreds of images. Is there such a method to be integrated with ruby on rails?

Thanks

Re: multiple uploads with attachment_fu II

You may want to check out MERB, I believe Ezra wrote it for something like this: http://brainspl.at/  Looks like he just released version 0.37 as a gem.  He talked about it being production ready at 0.4 when I saw him a couple of days ago.

17

Re: multiple uploads with attachment_fu II

Hey,

As mentioned before I'm uploading multiple images using the attachment fu. I want to add a drop down menu for each upload so that i can store a category for each image uploaded. Anyone has any suggestions?

<--create.rhtml-->
<%= error_messages_for :mugshot %>

<% form_for(:mugshot, :url => {:controller => 'mugshots', :action => 'create'},
              :html => { :multipart => true }) do |f| -%>
    <p>
        <label for ="mugshot">Upload A Mugshot:</label>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
        <p>
        <%= file_field_tag "image[]", :class => "formField" %>
    </p>
   
    <p>
        <%= submit_tag 'Upload' %>
    </p>
<% end -%>

Last edited by raj (2007-08-28 09:50:23)