2016-08-18 9 views
0

の添付ファイルが見つかりませんでした。私の添付ファイルはtest_suiteモデルに関連しています。私は自分のデータベースにファイルをアップロードすることができます。アップロードしたファイルの内容を確認できました。私は、ファイルが正しいかどうかを知りたいときに、サブメソッドとURLを実装しました。私の添付ファイルサーバにエラーがあります。 'id' =

def serve 
     @attachment = Attachment.find(params[:id]) # this is the line generating the error 
     send_data(@attachment.file_contents, :filename => "#{@attachment.attach_file_name}", 
              :type => @attachment.attach_content_type, 
              :size => @attachment.attach_file_size, 
              :disposition => "inline") 
end 

routes.rbを::

resources :test_suites do 
    resources :attachments, :only => [:create, :new, :destroy,:show] do 
     get "serve" # since serve is not a restfull route, it need to be under it's resource 
    end 
    end 

モデル/アタッチメントしかし、私は取得しています

コントローラ/ attachment_controller.rb " 'ID' =と添付ファイルが見つかりませんでした"。 RB:

class Attachement < ApplicationRecord 
    belongs_to :test_suite 
    has_attached_file :attach 

    validates_attachment_content_type :attach, :content_type => ["text/xml", "text/plain","text/html"], :message => 'File must be txt, xml, or html' 

    # create a function that sets the uploadedFile object attributes to our newly created file. 
    def attach=(attach) 
     # read allows us to process the data and read from it 
     self.file_contents = attach.read 
     self.attach_file_name = attach.original_filename 
     self.attach_content_type = attach.content_type 
     self.attach_file_size = attach.size 
    end 
end 

ビュー/ test_suites/show.html.erb:

<% test_suite.attachments.each do |attachment| %> 
    <p><%= link_to attachment.attach_file_name.split('.').first, test_suite_attachement_serve_path(test_suite,attachment)%> </p> 

<% end %> 
+1

ルートは 'に欠けているルート以下が生成されます

resources :test_suites do resources :attachments, :only => [:create, :new, :destroy,:show] do member do get :serve # since serve is not a restfull route, it need to be under it's resource end end end 

ブロック部材内にそれを追加します。「添付ファイル#は奉仕を」 '議論。コントローラーがAttachmentsControllerという名前であると仮定します。また、あなたの添付ファイルのモデルのスペルが間違っています。添付ファイル*添付ファイルでなければなりません。 – DiegoSalazar

答えて

0

これがあなたのために ``「サーブ」取得のための

/test_suites/attachments/:id/serve 
+0

これは私のために働いていません。メンバーを追加した後、リンクパスをserve_test_suite_attachement_path(test_suite、attachment)に変更しました。このエラーが発生しました。 "未初期化定数AttachementsController" –

+0

attachment_controllerファイルがなくなっています。 Atomを開いてブランチ間を切り替えると、何度か起こるようです。私はそれを今戻して、サーブが働いている。ありがとう。 –

関連する問題