ActiveStorage - 创建或更新附件

ActiveStorage - Create or Update attachment

我实际上正在开发一个 rails 应用程序,我在使用 ActiveStorage 时遇到了一些问题。

我的 rails 应用程序已上线,一切正常。 我的应用程序中有一个 model call Room,每个 Room 的所有者都可以添加一个 banner 图片

实际上,如果用户创建了一个新房间,他可以添加一个横幅图片,一切正常,很好。

我的第一个问题是安装Active Storage之前已经创建的房间,我无法添加横幅图像

我以为我可以重新使用我在编辑中新建时使用的表格,但是不行,我的附件是空的。

我的第二个问题:如果我的用户尝试更新图像,那是行不通的。用户必须先删除然后添加新的,从我的编辑页面。

我的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

我的房间控制器:

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

我的编辑表单:

<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 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

如果你有想法... 非常感谢您的帮助!

此致

我无法重现您的问题,但我对这一行有疑问:

after_save :purge_header_image, if: :remove_header_image

变量remove_header_image作为复选框的参数,所以它可以是"0""1",作为字符串,而不是falsetrue .这意味着它的计算结果总是 true.

所以我建议修改如下,看看是否可行:

after_save :purge_header_image

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