2016-08-18 28 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。 –

相关问题