Redmine How to upload and download a file inside a plugin?
I am trying to create a Redmine plugin and in this plugin I want to upload a file or image and also display an image or upload a file in the show action. can someone help me.
In the model
class UserInformation < ActiveRecord::Base
unloadable
belongs_to :project
acts_as_attachable :view_permission => :view_files,
:edit_permission => :manage_files,
:delete_permission => :manage_files
In the controller
class UserInformationsController < ApplicationController
unloadable
require File.dirname(__FILE__) + '/../../../../app/helpers/attachments_helper'
include AttachmentsHelper
helper :attachments
In new.html.erb
<p> <%= render :partial => 'attachments/form' %></p>
In show.html.erb
<%= link_to_attachments @info, :thumbnails => true %>
Can you help me, is this the right way?
source to share
Redmine already has classes for working with attachments - model Attachment
, controller AttachmentsController
and attachments and helpers.
You can use them in your classes.
Add acts_as_attachable...
with the required parameters to the model class after the line unloadable
. Possible options:
- Resolution options. For example
:view_permission => :view_attachments_permissions
, whereview_attachments_permissions
is a standard or legal plugin. If the user wants to download attachments, they must have a role with this permission, or this permission must be public (plugin permissions only - public options in the source code). - Behavior parameters (actions after adding, deleting, etc.).
- And maybe other options.
Add <%= render :partial => 'attachments/form' %>
to your views.
And call the method save_attachments
in the controller when it saves your model instance. Or add attachments manually after saving the instance:
params[:attachments].each do |attachment_param|
attachment = Attachment.where('filename = ?', attachment_param[1][:filename]).first
unless attachment.nil?
attachment.container_type = YourModel.name
attachment.container_id = set.id
attachment.save
end
end
Attachments are saved immediately after adding, but without information about the container
Also you can add attachments to existing redmine class using hotfix.
For example, I fixed the TimeEntry
class:
require_dependency 'time_entry'
module TimeEntryPatch
def self.included(base) # :nodoc:
base.send(:include, InstanceMethods)
base.class_eval do
unloadable # Send unloadable so it will not be unloaded in development
acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed
end
end
...
You can see examples directly in the Redmine code.
Attachments are used issue
, Project
and some other models. I found the answers to my questions there!
To view the attached images, you can use plugins such as Lightbox 2 . Add the plugin to Redmine or copy its code and stylesheets to your plugin.
source to share