Generate image url

0

I'm uploading images to my rails project (I'm not using gems), I upload them to public/temp up to here all right, the problem I have is that I need to generate a public url of the image so that the photo can be accessed for another service.

this is my code:

    base_url = "public/temp"
    require 'fileutils'
    FileUtils::mkdir_p base_url

    file_name = params[:image].original_filename
    path = File.join(base_url, file_name)
    File.open(path, "wb") { |f| f.write(params[:image].read) }

so far probe with this: ActionController::Base.helpers.path_to_image("#{base_url}/#{file_name}") but it does not work for me, this generates /images/public/temp/foto.jpg but I take it as a route of rails.

What I need is to generate something like this: https:my_url/url_imagen and that can be accessed from anywhere else.

This generates the log of my app:

{"status":404,"error":"Not Found","exception":"#\u003cActionController::RoutingError: No route matches [GET] \"/temp/foto.jpg\"\u003e","traces":{"Application Trace":[],"Framework Trace":[{"id":0,"trace":"actionpack (5.0.7) lib/action_dispatch/middleware/debug_exceptions.rb:53:in 'call'"},{"id":1,"trace":"actionpack (5.0.7) lib/action_dispatch/middleware/show_exceptions.rb:31:in 'call'"},{"id":2,"trace":"railties (5.0.7) lib/rails/rack/logger.rb:36:in 'call_app'"},{"id":3,"trace":"railties (5.0.7) lib/rails/rack/logger.rb:24:in 'block in call'"},{"id":4,"trace":"activesupport (5.0.7) lib/active_support/tagged_logging.rb:69:in 'block in tagged'"},{"id":5,"trace":"activesupport (5.0.7) lib/active_support/tagged_logging.rb:26:in 'tagged'"},{"id":6,"trace":"activesupport (5.0.7) lib/active_support/tagged_logging.rb:69:in 'tagged'"},{"id":7,"trace":"railties (5.0.7) lib/rails/rack/logger.rb:24:in 'call'"},{"id":8,"trace":"actionpack (5.0.7) lib/action_dispatch/middleware/request_id.rb:24:in 'call'"},{"id":9,"trace":"rack (2.0.5) lib/rack/runtime.rb:22:in 'call'"},{"id":10,"trace":"activesupport (5.0.7) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in 'call'"},{"id":11,"trace":"actionpack (5.0.7) lib/action_dispatch/middleware/executor.rb:12:in 'call'"},{"id":12,"trace":"actionpack (5.0.7) lib/action_dispatch/middleware/static.rb:136:in 'call'"},{"id":13,"trace":"rack (2.0.5) lib/rack/sendfile.rb:111:in 'call'"},{"id":14,"trace":"rack-cors (1.0.2) lib/rack/cors.rb:97:in 'call'"},{"id":15,"trace":"railties (5.0.7) lib/rails/engine.rb:522:in 'call'"},{"id":16,"trace":"puma (3.11.4) lib/puma/configuration.rb:225:in 'call'"},{"id":17,"trace":"puma (3.11.4) lib/puma/server.rb:632:in 'handle_request'"},{"id":18,"trace":"puma (3.11.4) lib/puma/server.rb:446:in 'process_client'"},{"id":19,"trace":"puma (3.11.4) lib/puma/server.rb:306:in 'block in run'"},{"id":20,"trace":"puma (3.11.4) lib/puma/thread_pool.rb:120:in 'call'"},{"id":21,"trace":"puma (3.11.4) lib/puma/thread_pool.rb:120:in 'block in spawn_thread'"}],"Full Trace":[{"id":0,"trace":"actionpack (5.0.7) lib/action_dispatch/middleware/debug_exceptions.rb:53:in 'call'"},{"id":1,"trace":"actionpack (5.0.7) lib/action_dispatch/middleware/show_exceptions.rb:31:in 'call'"},{"id":2,"trace":"railties (5.0.7) lib/rails/rack/logger.rb:36:in 'call_app'"},{"id":3,"trace":"railties (5.0.7) lib/rails/rack/logger.rb:24:in 'block in call'"},{"id":4,"trace":"activesupport (5.0.7) lib/active_support/tagged_logging.rb:69:in 'block in tagged'"},{"id":5,"trace":"activesupport (5.0.7) lib/active_support/tagged_logging.rb:26:in 'tagged'"},{"id":6,"trace":"activesupport (5.0.7) lib/active_support/tagged_logging.rb:69:in 'tagged'"},{"id":7,"trace":"railties (5.0.7) lib/rails/rack/logger.rb:24:in 'call'"},{"id":8,"trace":"actionpack (5.0.7) lib/action_dispatch/middleware/request_id.rb:24:in 'call'"},{"id":9,"trace":"rack (2.0.5) lib/rack/runtime.rb:22:in 'call'"},{"id":10,"trace":"activesupport (5.0.7) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in 'call'"},{"id":11,"trace":"actionpack (5.0.7) lib/action_dispatch/middleware/executor.rb:12:in 'call'"},{"id":12,"trace":"actionpack (5.0.7) lib/action_dispatch/middleware/static.rb:136:in 'call'"},{"id":13,"trace":"rack (2.0.5) lib/rack/sendfile.rb:111:in 'call'"},{"id":14,"trace":"rack-cors (1.0.2) lib/rack/cors.rb:97:in 'call'"},{"id":15,"trace":"railties (5.0.7) lib/rails/engine.rb:522:in 'call'"},{"id":16,"trace":"puma (3.11.4) lib/puma/configuration.rb:225:in 'call'"},{"id":17,"trace":"puma (3.11.4) lib/puma/server.rb:632:in 'handle_request'"},{"id":18,"trace":"puma (3.11.4) lib/puma/server.rb:446:in 'process_client'"},{"id":19,"trace":"puma (3.11.4) lib/puma/server.rb:306:in 'block in run'"},{"id":20,"trace":"puma (3.11.4) lib/puma/thread_pool.rb:120:in 'call'"},{"id":21,"trace":"puma (3.11.4) lib/puma/thread_pool.rb:120:in 'block in spawn_thread'"}]}}
    
asked by mariovzc 30.07.2018 в 18:48
source

2 answers

1

You could use some of the methods of the request instance available at the controller level. For example:

request.host # "localhost"
request.host_with_port # "localhost:3000"
request.url # "http://localhost:3000/"

Other options that you can use, you can find them in the rails documentation .

    
answered by 31.07.2018 / 22:29
source
0

Another option is to use root_url , which gives you the root URL of the application; in your case, because it is development, I would give you http://localhost:3000/ ; and you could use it like this:

"#{root_url}temp/#{params[:image].original_filename}"

Remember that you can request the files in the public directory through the root of your site; in your case, to request a file within public/temp , just use http://localhost:3000/temp .

    
answered by 01.08.2018 в 19:26