質問

I'm using paperclip to allow users to upload a logo, which is stored in the database

company_setting.rb model:

has_attached_file :logo_image,
      :storage => :database,
      :styles => {:original => "160x80>",
                  :small  => {:geometry =>"100x50>" , :column =>'logo_image_small_file'}},
      :url => '/settings/:id/logo_images?id=:id&style=:style'

I have no problem rendering this image to html as follows in a html.erb view

<%=  image_tag current_user.company.company_setting.logo_image.url + "&id=#{current_user.company.company_setting.id}" %>

However, I cannot get the same image to render in my pdf.erb file. I've previously experienced issues with pdfkit and the asset pipeline. I think pdfkit requires the full url to find an image so I've tried numerous ways such as:

<%=  image_tag "#{Rails.root}" + current_user.company.company_setting.logo_image.url + "&id=#{current_user.company.company_setting.id}" %>

Still not rendering. Anyone got any ideas?

役に立ちましたか?

解決

So it turns out that there were 2 problems with what I am trying to do here.

Firstly, the full url that I created was wrong. I changed the construction of the url for the image tag to:

<%=  image_tag URI.join(root_url(:subdomain => current_user.company.subdomain),(company_setting.logo_image.url + "&id=#{company_setting.id}"))).to_s%>

This allows for the subdomain to be added in front of the base url, whether in development or production. (Railscast here)

The second problem is that when the pdf is requested, a deadlock occurs, i.e. there is a call to the server for the logo image and for the pdf and neither can complete until the other is finished. I haven't come up with a solution to this that I'm satisfied with. It is solved by setting config.threadsafe! in the production.rb and development.rb files which effectively loads all resources and code before carrying out any server requests. This is fine in production, however setting this in the development.rb means that I can't carry out F5 driven development....the server needs to be restarted for each change to be seen in the browser. I'm looking into caching the image and serving it locally or figuring out threaded requests locally (using WeBrick server)

In the meantime, since I've set the pdf up and I don't really need to edit it (yet!) I have set up threadsafe in production and not in development with a conditional on the image:

<% if !Rails.env.development? %>
    <%=  image_tag URI.join(root_url(:subdomain......%>
<% end %> 

Not ideal but it will do for now....

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top