3

I'm using image_path into my controller:

class ArchivesController < ApplicationController
    include ActionView::Helpers::AssetUrlHelper

    def create
        @archive = Archive.new(file: params[:file])

        render json: {ico: image_path('docs/pdf.png')}
    end
end

But my return is {ico: '/images/docs/pdf.png'}.

When I'm use image_path('docs/pdf.png') in my views, the url is '/docs/pdf.png' and works.

Why in my controller the url is different?

  • What happens when you prepend a slash like this **'/docs/pdf.png'** in controller – Abhi Jul 13 '15 at 14:05
  • 2
    I'm not sure why they're different but you could use `view_context.image_path 'pdf.png'` without including the helper library. – vich Jul 13 '15 at 14:15
  • 1
    Thanks @mmichael!! With view_context.image_path worked. I didn't know I could use the view_context. Very, Very Thanks. – Bruno Mucelini Mergen Jul 13 '15 at 14:25
  • 1
    I believe you're in need of "ActionController::Base.helpers.asset_path(fileHere) . Checkout this post for more info: http://stackoverflow.com/questions/7827078/access-asset-path-from-rails-controller – bkunzi01 Jul 13 '15 at 14:25

1 Answers1

7

Answer from my comment:

You can use view_context.image_path 'pdf.png' without including the helper library.

vich
  • 11,836
  • 13
  • 49
  • 66