New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActionView::Template::Error (undefined method `variant' for an instance of ActiveStorage::Attached::One): #51123
Comments
Provide us a reproduction test script using this template - https://github.com/rails/rails/blob/main/guides/bug_report_templates/active_storage.rb |
i don't understand how to do it? |
For the sake of thoroughness here's what I used:
Here's the script: # frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails"
# If you want to test against edge Rails replace the previous line with this:
# gem "rails", github: "rails/rails", branch: "main"
gem "sqlite3"
end
require "active_record/railtie"
require "active_storage/engine"
require "tmpdir"
class TestApp < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f
config.root = __dir__
config.hosts << "example.org"
config.eager_load = false
config.session_store :cookie_store, key: "cookie_store_key"
config.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
config.active_storage.service = :local
config.active_storage.service_configurations = {
local: {
root: Dir.tmpdir,
service: "Disk"
}
}
end
ENV["DATABASE_URL"] = "sqlite3::memory:"
Rails.application.initialize!
require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s
ActiveRecord::Schema.define do
CreateActiveStorageTables.new.change
create_table :profiles, force: true
end
class Profile < ActiveRecord::Base
has_one_attached :photo do |attachable|
attachable.variant :thumb, resize_to_limit: [100, 100]
end
end
require "minitest/autorun"
class BugTest < Minitest::Test
ActiveStorage::Current.url_options = { host: "https://www.example.com" }
def setup
@profile = Profile.create!(
photo: {
content_type: "image/png",
filename: "rails-logo.png",
io: File.open("./rails-logo.png")
}
)
end
def test_attachment_url_methods_work
refute_nil @profile.photo.url
refute_nil @profile.photo.variant(:thumb)
end
# Fails since nothing is raised
def test_attachment_url_methods_throw_error
assert_raises NoMethodError do
@profile.photo.url
@profile.photo.variant(:thumb)
end
end
end The first test passes while the second fails because nothing is being raised. If there's some more info provided I can try to reproduce a different way or if there's an error with my testing method LMK. If you want to reproduce this script you'll need a PNG file adjacent to this script called Edit: Same result with Ruby However something else weird occurs:
Full Backtrace
|
i have in my model
After creating proflle
@profile.photo.attached?
=> true
[16] pry(#<#Class:0x00007f749628aac0>)>
[17] pry(#<#Class:0x00007f749628aac0>)>
[18] pry(#<#Class:0x00007f749628aac0>)> @profile.photo.url
NoMethodError: undefined method
url' for an instance of ActiveStorage::Attached::One from /home/lap-053/.rbenv/versions/3.3.0-dev/lib/ruby/gems/3.4.0+0/gems/activesupport- 7.1.3/lib/active_support/core_ext/module/delegation.rb:336 :in
method_missing'These methods not working please help.
The text was updated successfully, but these errors were encountered: