1

I have a Rails 5.2 app in which I am using activerecord attachments to upload photos as attachments.

They upload fine, but come out rotated. I have seen other Stack Overflow questions that deal with Paperclip uploads and this issue (like this one or this one), but since Paperclip is depreciated with Rails 5.2 the initializer they say to fix is not present.

Can anyone figure out what's going wrong here?

My form is like this:

  <%= simple_form_for [@capsule, CapsuleItem.new] do |f| %>
    <%= f.error_notification %>
    <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

    <div class="form-inputs">
      <div class="form-group">
        <%= f.label :item_date %>
        <input placeholder="Date of Photograph" type="text" id="date" class="form-control datepicker"></input>
        <%= f.hidden_field :item_date, id: "dateHolder", class: "hiddenDateField" %>
      </div> <!-- form group -->
      <%= f.input :photo %>
      <%= f.input :title %>
      <%= f.input :caption %>
      <%= f.hidden_field :capsule %>
    </div>

    <div class="form-actions text-center">
      <%= f.button :submit, value: "Add Photo to Time Capsule", class: "btn btn-danger" %>
    </div>
  <% end %>

My controller capsule_item#create method is here:

  def create
    @capsule = Capsule.find(params[:capsule_id])
    @capsule_item = @capsule.capsule_items.build(capsule_item_params)
    @capsule_item.user = current_user

    respond_to do |format|
      if @capsule_item.save
        format.html { redirect_to @capsule, notice: 'Capsule item was successfully created.' }
        format.json { render :show, status: :created, location: @capsule_item }
      else
        format.html { render :new }
        format.json { render json: @capsule_capsule_item.errors, status: :unprocessable_entity }
      end
    end
  end

And my CapsuleItem model is like this:

class CapsuleItem < ApplicationRecord
  belongs_to :user
  belongs_to :capsule
  has_one_attached :photo
end

The image is being displayed like this:

<%= image_tag rails_blob_url(item.photo.variant(auto_orient: true)), class: "card-img-top" %>
Liz
  • 1,369
  • 2
  • 26
  • 61
  • I'm pretty sure it is the same underlying issue as the duplicate. If using `capsule_item.photo.variant(auto_orient: true)` doesn't work then let me know I'll get this reopened. – mu is too short Feb 13 '19 at 04:22
  • @muistooshort Unfortunately, I'm using `<%= image_tag rails_blob_url(item.photo)" %>` to render the image, so adding `variant(auto_orient: true)` makes it throw up errors` – Liz Feb 13 '19 at 05:44
  • I've also tried putting it in the model, but haven't found a successful way to do so. – Liz Feb 13 '19 at 06:00
  • `image_tag item.photo.variant(auto_orient: true)` should do the trick. – mu is too short Feb 13 '19 at 08:05

0 Answers0