Ruby on rails 在我的控制器中发送带有身份验证的\u文件

Ruby on rails 在我的控制器中发送带有身份验证的\u文件,ruby-on-rails,ruby,redmine,Ruby On Rails,Ruby,Redmine,我使用ruby 1.9.3、Rails 4.0.4和Redmine 1.4.4 只有在我有权利的情况下,我才想下载PDF 我的控制器: class MyController < ApplicationController unloadable def myFunction # code that create a pdf File.open(myPdf, 'r') do |file| send_file file, :filename => m

我使用ruby 1.9.3、Rails 4.0.4和Redmine 1.4.4

只有在我有权利的情况下,我才想下载PDF

我的控制器:

class MyController < ApplicationController
  unloadable

  def myFunction
    # code that create a pdf

    File.open(myPdf, 'r') do |file|
      send_file file, :filename => myPdf, :type => "application/pdf", :disposition => "attachment"
      end
    end
  end

end
class MyControllermyPdf,:type=>application/pdf,:disposition=>attachment
结束
结束
结束
结束
如果我试图在没有登录的情况下访问我的PDF,我可以下载它,因此我需要检查身份验证

我尝试了
http\u basic\u authenticate\u with:name=>“name”,“password=>“psw”
,它给了我
(myController:Class的未定义方法“http\u basic\u authenticate\u with”)

我在动作:验证之前尝试了
,它为myController:Class提供了
未定义的“动作之前”方法

我在过滤之前尝试了
:authenticate\u user
…它为我提供了
未定义的方法“authenticate\u user”

我应该在操作之前使用
,因为它是Rails 4,为什么它不工作

我该怎么办


编辑:我不希望所有用户都能访问pdf,只希望我提供的那些用户能够访问…

我在我的项目中使用此功能:

class DownloadController < ApplicationController
  before_action :authenticate, only: :billing_pdf

  def billing_pdf
    # i save my pdf file in private folder
    send_file Rails.root.join('private', "billing_pdf"), type: "application/pdf", x_sendfile: true
  end

   def authenticate
    authenticate_or_request_with_http_basic('Administration') do |username, password|
      username == 'admin' && password == 'password'
    end
  end
end

是的,我错了我的类名,我编辑了我的帖子。你的解决方案在身份验证上循环(我复制/通过了它!),我做错了什么?但这不是我想要的(对不起,我说得很糟糕):调用我的控制器时带有指向的链接。我希望用户只有单击链接才能下载pdf,而无需其他方式。有可能吗?感谢简单的方法:我希望完全像RedMine中的pdf导出一样在我的项目中使用此功能,查看更新应答中使用的代码。
<%= link_to 'Download Billing', download_billing_pdf_path %>
namespace :download do
  get 'billing_pdf'
end