ruby-on-rails - Redmine 如何在插件中上传和下载文件?

标签 ruby-on-rails redmine redmine-plugins

我正在尝试创建 Redmine 插件,在该插件中我想上传文件或图像,并在显示操作中显示图像或下载文件。谁能帮帮我。

在模型中

class UserInformation < ActiveRecord::Base
  unloadable
  belongs_to :project

  acts_as_attachable :view_permission => :view_files,
                    :edit_permission => :manage_files,
                    :delete_permission => :manage_files

在 Controller 中

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

你能帮我看看这是正确的方法吗?

最佳答案

Redmine 已经有了处理附件的类 - 型号 Attachment , Controller AttachmentsController和附件 View 和助手。

您可以在自己的类(class)中使用它们。

添加acts_as_attachable...unloadable 之后为您的模型类提供必要的选项排。选项是:

  • 权限选项。例如 :view_permission => :view_attachments_permissions , 其中view_attachments_permissions是标准或插件权限。如果用户想要下载附件,他必须拥有该权限的角色,或者该权限必须是公开的(仅限插件权限 - 源代码中的公共(public)选项集)。
  • 行为选项(添加、删除等后的操作)。
  • 也许还有其他选择。

添加<%= render :partial => 'attachments/form' %>在您看来。

然后调用save_attachments保存模型实例时 Controller 中的方法。 或者实例保存后手动添加附件:

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 类。

例如,我修补了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
...

Redmine代码中可以直接看例子

附件由 issue 使用, Project和其他一些模型。 我在那里找到了问题的答案!


查看附加图片你可以使用像Lightbox 2这样的插件. 将插件添加到您的 Redmine 或将其代码和样式表复制到您的插件。

关于ruby-on-rails - Redmine 如何在插件中上传和下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31513862/

相关文章:

jquery - 可重复使用的远程模态 Rails

ruby-on-rails - 安装 ruby​​ debug19。编译器无法生成可执行文件

ruby-on-rails - 使所有格弦变轨?

mysql - RedmineMySQL 未启动

ruby-on-rails - 在 rails 应用程序中使用 LibreOffice 将 Doc 的前几页导出为 PDF

ruby - "Please install the mysql2 adapter"

ruby-on-rails - Redmine-插件开发: helper method not found in view

Redmine:新创建的子项目可以继承父项目的成员吗?

ruby - Ubuntu rake 中止! NameError:未初始化的常量 ActionDispatch::XmlParamsParser

redmine - 如何查看我在 redmine 中登录的所有项目的总小时数