update theme

This commit is contained in:
呱呱呱 2023-08-24 14:50:43 +08:00
parent 7139ae0613
commit c3a949906a
5 changed files with 39 additions and 18 deletions

View File

@ -1,4 +1,4 @@
class Admins::PageThemesController < Admins::Topic::BaseController
class Admins::PageThemesController < Admins::BaseController
before_action :finder_page_theme, only: [:edit, :update, :destroy]
def index
@ -19,9 +19,9 @@ class Admins::PageThemesController < Admins::Topic::BaseController
def create
@page_theme = PageTheme.new update_params
binding.pry
if @page_theme.save
save_image_file(params[:image_url], @page_theme)
save_image_file(params[:image])
redirect_to admins_page_themes_path
flash[:success] = "新增主题成功"
else
@ -58,4 +58,12 @@ class Admins::PageThemesController < Admins::Topic::BaseController
def update_params
params.require(:page_theme).permit(:language_frame, :name, :cate, :image_url, :clone_url)
end
def save_image_file(file)
return unless file.present? && file.is_a?(ActionDispatch::Http::UploadedFile)
file_path = Util::FileManage.source_disk_filename(@page_theme, "image")
File.delete(file_path) if File.exist?(file_path) # 删除之前的文件
Util.write_file(file, file_path)
end
end

View File

@ -54,8 +54,10 @@ class SitePagesController < ApplicationController
end
def themes
data = YAML.load_file(Rails.root.join('config/admins', 'page_themes.yml'))
render_ok({themes:data[theme_params.downcase]})
# data = YAML.load_file(Rails.root.join('config/admins', 'page_themes.yml'))
# render_ok({themes:data[theme_params.downcase]})
data = PageTheme.where(language_frame:theme_params).order(order_index: :asc)
render_ok ({themes:data})
end
private

View File

@ -15,4 +15,16 @@
class PageTheme < ApplicationRecord
enum language_frame: { hugo: 0, jeklly: 1, hexo: 2}
validates :name, presence: {message: "主题名不能为空"}, uniqueness: {message: "主题名已存在",scope: :language_frame}
def image
image_url('image')
end
private
def image_url(type)
return nil unless Util::FileManage.exists?(self, type)
Util::FileManage.source_disk_file_url(self, type)
end
end

View File

@ -38,16 +38,15 @@
<%= p.number_field :order_index, class: "form-control",placeholder: ""%>
</div>
<div class="logo-item">
<% logo_img = @page_theme.image_url %>
<% logo_img = @page_theme.image %>
<div class="logo-item-left mr-3 <%= logo_img ? 'has-img' : '' %>">
<img class="logo-item-img nav-logo-img" src="<%= logo_img %>" style="<%= logo_img.present? ? '' : 'display: none' %>"/>
<%= file_field_tag(:image_url, accept: 'image/png,image/jpg,image/jpeg',style: "display: none", value: params[:image]) %>
<label for="image_url" class="logo-item-upload" data-toggle="tooltip" data-title="选择图片"></label>
<%= file_field_tag(:image, accept: 'image/png,image/jpg,image/jpeg',style: "display: none", value: params[:image]) %>
<label for="image" class="logo-item-upload" data-toggle="tooltip" data-title="选择图片"></label>
</div>
<div class="logo-item-right">
<div class="logo-item-right">
<div class="logo-item-title flex-1">logo</div>
<div>格式PNG、JPG</div>
<div>尺寸高度38px以内宽等比例缩放</div>
</div>
</div>
</div>

View File

@ -11,16 +11,16 @@
</thead>
<tbody>
<% if page_themes.present? %>
<% page_themes.each_with_index do |banner, index| %>
<tr class="page-theme-item-<%= banner.id %>">
<% page_themes.each_with_index do |theme, index| %>
<tr class="page-theme-item-<%= theme.id %>">
<td><%= list_index_no((params[:page] || 1).to_i, index) %></td>
<td><%= banner.language_frame %></td>
<td><%= banner.name %></td>
<td><img style="width:150px" src="<%= banner.image_url %>" /></td>
<td><a href="<%= banner.clone_url %>" target="_blank"><%= banner.clone_url %></a> </td>
<td><%= theme.language_frame %></td>
<td><%= theme.name %></td>
<td><img style="width:150px" src="<%= theme.image %>" /></td>
<td><a href="<%= theme.clone_url %>" target="_blank"><%= theme.clone_url %></a> </td>
<td class="action-container">
<%= link_to "编辑", edit_admins_page_theme_path(banner), remote: true, class: "action" %>
<%= link_to "删除", admins_page_theme_path(banner), method: :delete, data:{confirm: "确认删除的吗?"}, class: "action" %>
<%= link_to "编辑", edit_admins_page_theme_path(theme), remote: true, class: "action" %>
<%= link_to "删除", admins_page_theme_path(theme), method: :delete, data:{confirm: "确认删除的吗?"}, class: "action" %>
</td>
</tr>
<% end %>