我实际上正在开发一个rails应用程序,我在使用ActiveStorage时遇到了一些麻烦。
我的 rails 应用程序在线,一切正常。 我的应用中有一个模型调用"房间",每个"房间"的所有者都可以添加横幅图像。
实际上,如果用户创建一个新的房间,他可以添加一个横幅图像,一切正常,这很好。
我的第一个问题是在安装活动存储之前已经创建的房间,我无法添加横幅图像。
我以为我可以重复使用我在编辑中用于 New 的表单,但不能,我的附件是 nill。
我的第二个问题:如果我是一个用户尝试更新图像,那是行不通的。用户必须先删除,然后从我的编辑页面添加一个新页面。
我的房间.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"
,作为字符串,而不是false
或true
。这意味着它的评估始终为true
.
所以我建议更改如下,看看它是否有效:
after_save :purge_header_image
private
def purge_header_image
header_image.purge_later if remove_header_image == '1'
end