Search code examples
ruby-on-railsmodelupdatesattachmentrails-activestorage

ActiveStorage - Create or Update attachment


I’m actually working on a rails app and I have some troubles with ActiveStorage.

My rails app is online, everything work perfectly. I have a model call Room in my app, and each owner of a Room can add a banner image.

Actually if a user create a new Room, he can add a banner image, everything work, that’s good.

My first problem is for the Room already created before I install Active Storage, I can not add banner image.

I thought I can re-use the form i used for New in Edit but not, my attachment is nill.

My second problem : if I a user try to update the image, that’s doesn’t work. The user has to delete first and then add a new one, from my edit page.

My Room.rb :

class Room < ApplicationRecord
  validates :title, uniqueness: true, presence: true, length: {maximum: 50}
  validates :description, uniqueness: true, presence: true, length: {maximum: 200}
  validates :theme, presence: true


  belongs_to :user
  belongs_to :theme

  has_many :subjects, dependent: :destroy
  has_many :rubrics, dependent: :destroy
  has_many :favorites, dependent: :destroy

  has_one_attached :header_image

  attr_accessor :remove_header_image

  # delette header image
  after_save :purge_header_image, if: :remove_header_image
  private def purge_header_image
    header_image.purge_later
  end

  # Consider room as searchable
  searchkick searchable: [:title]
end

my Room controller :

class RoomsController < ApplicationController

  def index
    # @rooms = Room.all
    @rooms = policy_scope(Room)
  end

  def new
    @room = Room.new
    authorize @room
  end

  def show
    @room = Room.find(params[:id])
    @favorite_exists = Favorite.where(room: @room, user: current_user) == [] ? false : true
    if params[:rubric_id]
      @subject = Subject.where(room: @room, rubric_id: params[:rubric_id])
    else
      @subject = Subject.where(room: @room)
    end
    authorize @room
    @footer_variable = true
  end

  def create
    @room = Room.new(room_params)
    @room.user_id = current_user.id
    authorize @room
    if @room.save
      redirect_to new_room_rubric_path(@room)
      RoomMailer.creation_confirmation(@room).deliver_now
    else
      render :new
    end
  end

  def edit
    @room = Room.find(params[:id])
    authorize @room
  end

  def update
    @room = Room.find(params[:id])
    @room.update(room_params)
    redirect_to room_edit_room_rubric_path(@room)
    authorize @room
  end

private

  def room_params
    params.require(:room).permit(:title, :description, :theme_id, :header_image, :remove_header_image)
  end
end

my Edit form :

<div class="col-sm-7">
      <%= simple_form_for [@room], :html => { class: 'form-create'} do |f| %>
  <!-- form row 1 -->
      <span><p class="box"><i class="fas fa-clipboard-check"></i></p></span>
      <%= f.association :theme, input_html: {class: 'mt-15-n fa-select'}, prompt: 'Selectionnez un thème', collection: Theme.all, label_method: :title, value_method: :id, label: "Theme" %>
  <!-- form row 2 -->
      <span><p class="box"><i class="fas fa-rocket"></i></p></span>
      <%=f.input :title, input_html: {class: 'mt-15-n' ,:rows => 4}, :placeholder => 'Nom de votre communauté' %>
  <!-- edit header image -->
      <span><p class="box"><i class="far fa-images"></i></p></span>
  <!-- actual image -->
      <% if @room.header_image.attached?  %>
        <p class="description">Votre bannière actuelle</p>
        <%= image_tag @room.header_image.variant(resize: "300x300") %>
   <!--  delete image -->
        <p class="description">Supprimer la bannière.</p>
        <%=f.check_box :remove_header_image  %>
        <% else %>
        <p>Vous n'avez pas de banière, ajoutez une banière ci-dessous :</p>
        <%=f.file_field :header_image, :class => "asterisk" %>
      <% end %>
    <!-- form row 3 -->
      <div class="mt-20">
        <span><p class="box"><i class="fas fa-bars"></i></p></span>
        <%= f.input :description, :input_html => {class: 'mt-15-n', :rows => 5}, :maxlength =>"200", :placeholder => 'Description', hint: "Max 200 characters" %>
        <%= f.submit "Suivant", class: "btn-primary" %>
        <% end %>
      </div>
    </div>

Rooms Routes :

rooms GET   /rooms(.:format)          rooms#index
          POST  /rooms(.:format)          rooms#create
 new_room GET   /rooms/new(.:format)      rooms#new
edit_room GET   /rooms/:id/edit(.:format) rooms#edit
     room GET   /rooms/:id(.:format)      rooms#show
          PATCH /rooms/:id(.:format)      rooms#update
          PUT   /rooms/:id(.:format)      rooms#update

If you have an Idea ... 🙏🏻 Thanks a lot for your help !

Regards


Solution

  • I was not able to reproduce your problems, but I had an issue with this line:

    after_save :purge_header_image, if: :remove_header_image
    

    The variable remove_header_image comes as parameter from the checkbox, so it can be "0" or "1", as String, not false or true. It means it evaluates always to true.

    So I suggest to change as follows and see if it works:

    after_save :purge_header_image
    
    private
      def purge_header_image
        header_image.purge_later if remove_header_image == '1'
      end