Redmine 如何在插件中上传和下载文件?
Redmine How to upload and download file inside plugin?
我正在尝试创建 Redmine 插件,我想在该插件中上传文件或图像,并在显示操作时显示图像或下载文件。谁能帮帮我
模型中
class UserInformation < ActiveRecord::Base
unloadable
belongs_to :project
acts_as_attachable :view_permission => :view_files,
:edit_permission => :manage_files,
:delete_permission => :manage_files
在控制器中
class UserInformationsController < ApplicationController
unloadable
require File.dirname(__FILE__) + '/../../../../app/helpers/attachments_helper'
include AttachmentsHelper
helper :attachments
在new.html.erb
<p> <%= render :partial => 'attachments/form' %></p>
在show.html.erb
<%= link_to_attachments @info, :thumbnails => true %>
你能帮我看看这是正确的方法吗?
我已经在这个插件中做到了:https://github.com/iridakos/release_logs。查看相关代码。
Redmine 已经有 classes 用于处理附件 - 模型 Attachment
、控制器 AttachmentsController
和附件视图和助手。
您可以在自己的 classes 中使用它们。
在 unloadable
行之后将 acts_as_attachable...
和必要的选项添加到您的模型 class 中。选项是:
- 权限选项。例如
:view_permission => :view_attachments_permissions
,其中 view_attachments_permissions
是标准或插件权限。如果用户想要下载附件,他必须具有该权限的角色,或者该权限必须是 public(仅插件权限 - public 源代码中的选项集)。
- 行为选项(添加、删除等后的操作)。
- 也许还有其他选择。
在您的观点中添加<%= render :partial => 'attachments/form' %>
。
并在保存模型实例时调用控制器中的 save_attachments
方法。
或者实例保存后手动添加附件:
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
附件添加后立即保存,但没有容器信息
您还可以通过修补将附件添加到现有的 redmine class。
比如我打补丁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
...
Redmine代码中可以直接看例子
附件被issue
、Project
和其他一些模型使用。
我在那里找到了问题的答案!
查看附加图片您可以使用 Lightbox 2 等插件。
将插件添加到您的 Redmine 或将其代码和样式表复制到您的插件。
我正在尝试创建 Redmine 插件,我想在该插件中上传文件或图像,并在显示操作时显示图像或下载文件。谁能帮帮我
模型中
class UserInformation < ActiveRecord::Base
unloadable
belongs_to :project
acts_as_attachable :view_permission => :view_files,
:edit_permission => :manage_files,
:delete_permission => :manage_files
在控制器中
class UserInformationsController < ApplicationController
unloadable
require File.dirname(__FILE__) + '/../../../../app/helpers/attachments_helper'
include AttachmentsHelper
helper :attachments
在new.html.erb
<p> <%= render :partial => 'attachments/form' %></p>
在show.html.erb
<%= link_to_attachments @info, :thumbnails => true %>
你能帮我看看这是正确的方法吗?
我已经在这个插件中做到了:https://github.com/iridakos/release_logs。查看相关代码。
Redmine 已经有 classes 用于处理附件 - 模型 Attachment
、控制器 AttachmentsController
和附件视图和助手。
您可以在自己的 classes 中使用它们。
在 unloadable
行之后将 acts_as_attachable...
和必要的选项添加到您的模型 class 中。选项是:
- 权限选项。例如
:view_permission => :view_attachments_permissions
,其中view_attachments_permissions
是标准或插件权限。如果用户想要下载附件,他必须具有该权限的角色,或者该权限必须是 public(仅插件权限 - public 源代码中的选项集)。 - 行为选项(添加、删除等后的操作)。
- 也许还有其他选择。
在您的观点中添加<%= render :partial => 'attachments/form' %>
。
并在保存模型实例时调用控制器中的 save_attachments
方法。
或者实例保存后手动添加附件:
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
附件添加后立即保存,但没有容器信息
您还可以通过修补将附件添加到现有的 redmine class。
比如我打补丁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
...
Redmine代码中可以直接看例子
附件被issue
、Project
和其他一些模型使用。
我在那里找到了问题的答案!
查看附加图片您可以使用 Lightbox 2 等插件。 将插件添加到您的 Redmine 或将其代码和样式表复制到您的插件。