Argent
Argent

Reputation: 293

Rails 4 Asset Pipeline link

While using the asset pipeline in production and trying to keep the folder less cluttered, I have set up nested folders. I am trying to link to a pdf within a sub-folder, but I am not sure how to call the file precisely.

<%= link_to ' | PDF', asset_path(pub.file), :target => "_blank" %>

This works in development. For production I will need to call pubfiles/pub.file, but that syntax seems like it is missing something.

I figured out it should be something like this:

<%= link_to ' | PDF', asset_path("pubfiles/#{pub.file}"), :target => "_blank" %>

But when I try to use the link I error out with:

Missing template people/show, application/show with {:locale=>[:en], :formats=>[:pdf], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/rails/neuro/app/views"

def find(*args)
  find_all(*args).first || raise(MissingTemplate.new(self, *args))
end

def find_all(path, prefixes = [], *args)

In development without the folder argument setup, the pdf opens without a hitch.

Upvotes: 4

Views: 85

Answers (1)

Cristiano Mendon&#231;a
Cristiano Mendon&#231;a

Reputation: 1282

When using asset_path, rails will prepend /assets/ to the path you specified as its argument.

Thus make sure your sub-folder is within assets folder this way:

-public
  -assets
    -yourFolder
      -yourFile.ext

That way, you can use asset_path as asset_path('yourFolder/yourFile.ext') and rails will serve your file at /assets/yourFolder/yourFile.ext

Please notice that rails adds fingerprint to files in order to favor caching. Make sure your assets is properly compiled and configuration for production is properly defined.

Upvotes: 0

Related Questions