2015-07-20 219 views
3

我想創建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 %> 

你能幫我是正確的方法?

回答

2

Redmine已經有附件工作類 - 型號Attachment,控制器AttachmentsController和附件視圖和助手。

你可以在你自己的課堂上使用它們。

將行後的acts_as_attachable...加上您的模型類的neccesary選項。選項有:

  • 權限選項。例如:view_permission => :view_attachments_permissions,其中view_attachments_permissions是標準或插件許可。如果用戶想下載附件,他必須具有該權限的角色,或者該權限必須是公共的(只有插件權限 - 源代碼中的公共選項集)。
  • 行爲選項(添加,刪除等操作)。
  • 也許還有其他選擇。

在您的意見中添加<%= 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 

附件添加後立即撲救,但沒有容器信息


你也可以通過打補丁添加附件,以現有的技術管理平臺類。

例如,我修補TimeEntry類:

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 
... 

你可以看一下例子直接在管理平臺的代碼。

附件使用issueProject和一些其他模型。 我在那裏找到了我的問題的答案!


要查看附加圖片您可以使用插件,如Lightbox 2。 將插件添加到您的Redmine或將其代碼和樣式表複製到您的插件。

+0

哇這爲我工作。現在我能夠上傳文件,但如何下載文件。我面臨錯誤您無權訪問此頁面。現在看到我按照你的建議添加了模型代碼 –

+0

我最近添加了附件,並且仍然有相同的錯誤 我還沒有弄清楚,但很可能它是通過編輯init.rb中的權限來解決的'文件: ... project_module:your_plugin做 權限:PERMISSION_NAME,{:your_model => [:actions_array]}:公衆=>真正 結束 .... 外觀http://www.redmine.org/projects/redmine/wiki/Plugin_Tutorial#Adding-new-permissions –

+0

在添加init權限後面臨同樣的問題。 –