Search code examples
ruby-on-railsrails-activestorage

rails_blob_path host: and only_path: true returns the same?


Im trying to figure out how to use Rails.application.routes.url_helpers.rails_blob_path

I have an class with

has_one_attached :logo

and then I do

Rails.application.routes.url_helpers.rails_blob_path(record.logo)

It gives me an error

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true (ArgumentError)

Ok so I try

Rails.application.routes.url_helpers.rails_blob_path(obi.logo, host: "wwww.example.com" )
=> "/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--5d977f0c66fecd8c8474355813aad9490fcdfbf2/picture.png"

Which is the same outcome as

Rails.application.routes.url_helpers.rails_blob_path(obi.logo, only_path: true )
=> "/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--5d977f0c66fecd8c8474355813aad9490fcdfbf2/picture.png"

When using the host: parameter I would expect it to attach "www.example.com" to the output. Why is it the same outcome as only_path: true ?


Solution

  • When using the host: parameter I would expect it to attach "www.example.com" to the output. Why is it the same outcome as only_path: true ?

    In Rails there are two types of URL-helpers -- path and url

    With suffix _path -- relative URL path

    With suffix _url -- absolute URL

    Rails.application.routes.url_helpers.rails_blob_url(obi.logo, host: "https://www.example.com")
    
    # => "https://www.example.com/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--5d977f0c66fecd8c8474355813aad9490fcdfbf2/picture.png"