我实际上正在使用Rails应用程序,但是ActiveStorage遇到了一些麻烦。
我的Rails应用程序在线,一切正常。 我的应用程序中有一个呼叫室,并且每个会议室的所有者都可以添加横幅图片。
实际上,如果用户创建一个新的会议室,他可以添加横幅图像,一切正常,这很好。
我的第一个问题是在安装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
如果您有想法... 非常感谢您的帮助!
致谢
答案 0 :(得分:0)
我无法重现您的问题,但是此行出现了问题:
after_save :purge_header_image, if: :remove_header_image
变量remove_header_image
作为复选框的参数,因此它可以是"0"
或"1"
,而不是false
或true
,而不是String。这意味着它将始终评估为true
。
所以我建议进行如下更改,看看是否可行:
after_save :purge_header_image
private
def purge_header_image
header_image.purge_later if remove_header_image == '1'
end