To_Model Delegated to Attachment, But Attachment Is Nil

Can't resolve image into URL: to_model delegated to attachment, but attachment is nil in Rails 5.2

You can get the error message "Can't resolve image into URL: to_model delegated to attachment, but attachment is nil" if you are trying to show in your view an attachment that does not exist:

<%= image_tag(@user.avatar) %>

to avoid error you should do this:

<%= image_tag(@user.avatar) if @user.avatar.attached? %>

ActiveStorage Image Upload Error with Rails 5.2: signed_id delegated to attachment, but attachment is nil

I just ran into this error and really struggled to figure out what could have been happening. It first appeared when I submitted a form and did not include an attachment. Turns out I needed to check to see if something was really attached and deal with that possibility.

Perhaps try moving @blog.pdf.attach(params[:pdf]) to before the respond_to in blog#create

Then, when trying to show the image, maybe you could try something like this

<% if blog.pdf.attached? == false %>
<p>No pdf attached</p>
<% elsif blog.pdf.previewable? %>
<%= link_to(image_tag(blog.pdf.preview(resize: "50x50>")), rails_blob_path(blog.pdf, disposition: "attachment"))
%>
<% elsif blog.pdf.variable? %>
<%= link_to(image_tag(blog.pdf.variant(resize: "50x50")), rails_blob_path(blog.pdf, disposition: "attachment"))%>
<% else %>
<%= link_to "Download file", rails_blob_path(@blog.pdf, disposition: "attachment") %>
<% end %>

Heroku has a good article on active storage here that may also help.

Attach Cloudinary image to Rails model using ActiveStorage

Assuming your app/models/photo.rb looks similar to this:

class Photo < ActiveRecord::Base
attr_accessible :title, :bytes, :image, :image_cache

belongs_to :album

mount_uploader :image, ImageUploader

validates_presence_of :title, :image
end

What happens if you try:

...
user = User.new(title: "Chris")
user.photo.image = preloaded_file # <---- assign file to image attribute
user.save

You can also try to emulate this sample app for your case: https://github.com/cloudinary/cloudinary_gem/tree/master/samples/photo_album

EDIT: you can try something like this:

require 'uri'

file = URI.open(user_img_response["url"]) # use cloudinary url
photo.image.attach(io: file, filename: 'image.jpg')

See: https://blog.eq8.eu/til/upload-remote-file-from-url-with-activestorage-rails.html

Image not displayed in Outlook in ASP

Outlook web access will block any images by default - only if the user chooses to display/download images these will be displayed/downloaded. I am not sure if it is possible to adjust the default behavior by using office 365 admincenter or the OWA settings.

Some time ago it was possible to work around this by using it as a background image inside a table>tr>td cell background-image css property.

EDIT

Checked a recent project of myself, where we are sending notification mails about tickets. The site logo is displayed correctly in outlook/owa - without adding the recipient to the trusted list:

            using (MailMessage mm = new MailMessage(sender, header.RecipientAddress, header.Subject, header.Body))
{
mm.Body = header.Body;
mm.BodyEncoding = Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mm.Priority = priority == IntMailPriority.High ? MailPriority.High : MailPriority.Normal;
mm.IsBodyHtml = bodyIsHtml;

// logo
if (bodyIsHtml)
{
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(header.Body, Encoding.UTF8, "text/html");

string logoPath = $"{AppDomain.CurrentDomain.BaseDirectory}\\images\\logo_XXX.png";
LinkedResource siteLogo = new LinkedResource(logoPath)
{
ContentId = "logoId"
};
htmlView.LinkedResources.Add(siteLogo);
mm.AlternateViews.Add(htmlView);
}

// create smtpclient
SmtpClient smtpClient = new SmtpClient(smtpSettings.ServerAddress, smtpSettings.Port)
{
Timeout = 30000,
DeliveryMethod = SmtpDeliveryMethod.Network
};
// set relevant smtpclient settings
if (smtpSettings.UseTlsSsl)
{
smtpClient.EnableSsl = true;

// needed for invalid certificates
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
}
if (smtpSettings.UseAuth)
{
smtpClient.UseDefaultCredentials = false;

NetworkCredential smtpAuth = new NetworkCredential { UserName = smtpSettings.Username, Password = smtpSettings.Password };
smtpClient.Credentials = smtpAuth;
}

smtpClient.Timeout = smtpSettings.SendingTimeout * 1000;

// finally sent mail \o/ :)
try
{
smtpClient.Send(mm);
}
catch (SmtpException exc)
{
throw new ProgramException(exc, exc.Message);
}
catch (InvalidOperationException exc)
{
throw new ProgramException(exc, exc.Message);
}
catch (AuthenticationException exc)
{
throw new ProgramException(exc, exc.Message);
}
}

Afterwards the logo is referred to as

<IMG alt="Intex Logo" src="cid:logoId">

inside the generated html.



Related Topics



Leave a reply



Submit