Problem with generating attachments on-the-fly.

Added by Mario Scondo about 8 years ago


I'm trying to create an attachment programmatically. But I have had not success, because of missing documentation of the API.

My development Environment:

  Redmine version                          2.2.4.stable
  Ruby version                             1.8.7 (x86_64-linux)
  Rails version                            3.2.13
  Environment                              development
  Database adapter                         MySQL

When generating attachments from data send by a custom web form there is no problem. Attachments are created the Redmine-way and they are attached to the journal and to emails without any problems.

But when I try to add my own files to the list of attachments I am not successful. Here is an code example:

# fetch attachments from request generated by form
attachments = Attachment.attach_files(@issue, params[:attachments])[:files]

# create file and place it at Rails.root.join('files')
# rma_file contains the fqfn of the file to be attached
rma_file = generate_rma_file(opts)

# create attachment 
# Depending on the way I do assemble the attachment I do get
# request status 500 because of unsupported object interfaces or
# subsequent modules like the mailer in this case cannot handle 
# the list of attachments.
rma_attachment = Attachment.create(
                :container => @issue,
                :file => rma_file,
                :author => User.current)

# here I am trying to extend the list of attachments
attachments << Attachment.attach_files(@issue, rma_attachment)

The error message:

Completed 500 Internal Server Error in 1185ms

NoMethodError (undefined method `diskfile' for #<Hash:0x7fd4ca92a5d0>):
  app/models/mailer.rb:428:in `initialize'
  app/models/mailer.rb:441:in `method_missing'

I did try to get some ideas how to do that job from . But without success. Additionally I did take a look at the source codes of Redmine, but all the examples are working with datastructures recieved by a web request.

The example from the previous link is working with a file object to create the Attachment. I also tried this with the result, that the file object does not support the method call '.size()'. In that case the error has been thrown by the following code block:

  def file=(incoming_file)
    unless incoming_file.nil?
      @temp_file = incoming_file
      if @temp_file.size > 0   <-------- results in an error message

Are there any ideas or example source codes available regarding this problem?

Replies (2)

RE: Problem with generating attachments on-the-fly. - Added by Mario Scondo about 8 years ago

Uhh, that's a lot of information in the Post.

In summary, I am interested in an example how to create an object of Attachment referencing a file at the local filesystem to be able to attach it to an issue and an email.

RE: Problem with generating attachments on-the-fly. (solved) - Added by Mario Scondo about 8 years ago

I was able to solve the problem. There are a number of Redmine helpers and controllers without any line of comments and there are procedures that return strange results, when called twice.

The List of attachments has to be assembled manually and the number of required attributes to create a (custom) Attachment was quite large... but without any comments :-(