我有我的附件模型與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 %>
路線'讓「服務」'缺少'來的路線:「附件#服務」 「論點。假設控制器名爲AttachmentsController。另外,您的附件模型拼寫錯誤Attach * e * ment應該是Attachment。 – DiegoSalazar