Redmine Tips: How to add an attachment using rails c
This post describes how to add an attachment (file) to an issue using rails c
(rails console).
Note that it is assumed that issue
is already present.
This would work even if the plugins such as redmine_s3 installed.
Add an attachment to an issue w/ journal
# issue, user, filepath are assumed to be already set.
File.open(filepath, "rb") do |f|
# f.flock(File::LOCK_SH)
attachment = Attachment.new(
:file => ActionDispatch::Http::UploadedFile.new(
:tempfile => f,
:filename => File.basename(filepath),
:type => "image/jpeg",
),
:author => author,
)
issue.init_journal(author)
issue.attachments << attachment
issue.save
end
Add an attachment to an issue w/o journal
# issue, user, filepath are assumed to be already set.
File.open(filepath, "rb") do |f|
# f.flock(File::LOCK_SH)
Attachment.create!(
:file => ActionDispatch::Http::UploadedFile.new(
:tempfile => f,
:filename => File.basename(filepath),
:type => "image/jpeg",
),
:author => author,
:container => issue,
)
end
Note: Unlike “w/ journal” case, you need to set :container
to the issue.