2016-08-18 14 views
0

我有我的附件模型與test_suite模型相關。我可以將文件上傳到我的數據庫。我能夠看到我上傳的文件的內容。我實現了一個服務方法和URL,當我想要看到該文件是正確的。但是,我得到 「無法找到 '身份證'=附件」我的附件服務發生錯誤,無法找到附件'ID'=

控制器/ attachment_controller.rb:

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 

模型/附件。 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

路線'讓「服務」'缺少'來的路線:「附件#服務」 「論點。假設控制器名爲AttachmentsController。另外,您的附件模型拼寫錯誤Attach * e * ment應該是Attachment。 – DiegoSalazar

回答

0

在成員塊添加它

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 

,這將產生以下爲您

/test_suites/attachments/:id/serve 
+0

這不適合我。在我添加成員之後,我將鏈接路徑更改爲serve_test_suite_attachement_path(test_suite,attachment),現在我得到了此錯誤「未初始化的常量AttachementsController」 –

+0

我發現我的attachment_controller文件已經消失。當我打開Atom並在分支之間切換時,似乎發生了一些情況。我現在回來了,服務正在工作。非常感謝Deepak。 –

相關問題