我有我的附件模型与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