Adding Alt Tags To Uploaded Images...

Have you started a site, uploaded stuff like THIS and then remembered that alt tags are important for SEO? No? Well then you wouldn’t be here you liar.
You might want to check out my previous post about Shrine to see what I’m really working with here, but we’re just going to dive right in. 
What You (I) Did
 Typical us. Started uploading and firing stuff off before you should have. ‘But I really needed to get my creativeness OUT, yah know?’ You’re probably saying. Well you’re wrong. Measure twice, code 13 times, not 24. So we’ve got all these uploaded images sitting in our database. This isn’t a method to go give them all alt tags. We are just giving them spots for alt tags and displaying them.
How To Do It
 First up we gotta migrate some things. We just need to add a column to the model that is displaying the picture. There is probably a different way to do it where the alt tag is stored with the image but this is fast and dirty. And my images are single use.
 rails g migration AddImageAltTagToPosts image_alt_tag:string rails db:migrate
 and that should take care of that. Now in your Post form you’ll have to add in an ‘image_alt_tag’ field under the file field.
 ##/views/posts/_form.html.erb 
 …
 …
 <div class="field">
  <%=form.label :image %>
  <%=form.file_field :image %>
 </div>
 <div class="field">
  <%=form.label :image_alt_tag %>
  <%=form.text_field :image_alt_tag %>
 </div>
  <%-if post.image_data? %>
   <div class="field">
   Remove attachment: <%=form.check_box :remove_image %>
  <%-end %>
 </div>
 
 boom, your form should look something like this;
 Perfect, now anywhere you have image_tags for your uploaded images, just add in the following;
 <%=image_tag(post.image_url(:thumbnail), {class: 'class-here', alt: post.image_alt_tag}) %>
 REMEMBER! If you have more than one data field (class, alt, style) they need to be wrapped in curly braces! And to save yourself from some nil errors or empty fields, use this one;
alt: (@post.image_alt_tag.nil? ? @post.short_description @post.image_alt_tag)

 And there you go! Happy alt tag adding!

Comments (Coming Soon)