Ryo Masuda
Ryo Masuda

Reputation: 41

I got NoMethodError: undefined method `rails_blob_path` when using active storage

I got a following error when using active storage in rails 6.

NoMethodError: undefined method `rails_blob_path' for #<Module:0x00007fb91cb006a8>

My env

I already installed active storage and execute migrate

config/environments/development.rb

config.active_storage.service = :local

Model

class Contract < ApplicationRecord
  ...
  has_one_attached :original_file
end

Serializer

class ContractSerializer < ActiveModel::Serializer
  include Rails.application.routes.url_helpers

  attributes :id, :original_file

  def original_file
    Rails.application.routes.default_url_options[:host] = 'localhost:3000'
    Rails.application.routes.url_helpers.rails_blob_path(object.original_file, only_path: true) if object.original_file.attached?
  end
end

If you have any tips, Please let me know if you know anything about this. Thank you.

Upvotes: 3

Views: 5498

Answers (2)

Mephistt
Mephistt

Reputation: 186

The include statement and the full path to rails_blob_path method seem to be OK.

It looks like there is a naming issue. You have created attribute and method with the same name. Try to change one of them.

Upvotes: 1

Guillaume
Guillaume

Reputation: 1537

You should do like this :

Rails.application.routes.url_helpers.rails_representation_url(object.original_file(resize: "300x300").processed, only_path: true)

Source : https://stackoverflow.com/a/53547638/2679301

Upvotes: 0

Related Questions