我正在尝试使我的 rails 应用程序的徽标可互换,请参阅此处的上一篇文章
我有一个徽标模型,它具有以下字段名称:字符串、图像:字符串、默认值:布尔值
我正在使用 carrierwave gem 将图像上传到 image:string 列。
应用程序/模型/logo.rb
class Logo < ActiveRecord::Base
mount_uploader :image, LogoUploader
scope :default, where(default: true)
def falsify_all_others
Logo.where('id != ?', self.id).each do |item|
item.default = false
item.save
end
end
end
我有一个徽标控制器,在这里可以看到
app/controllers/logos_controller.rb
class LogosController < ApplicationController
def new
@logo= Logo.new
end
def create
@logo = Logo.new(params[:logo])
if @logo.save
@logo.falsify_all_others
redirect_to logos_path, :notice => "Logo has been uploaded successfully"
else
render "new"
end
end
def edit
@logo = Logo.find(params[:id])
end
def destroy
@logo = Logo.find(params[:id])
@logo.destroy
flash[:notice] = "Logo has been obliterated!"
redirect_to logos_path
end
def update
@logo = Logo.find(params[:id])
if @logo.update_attributes(params[:logo])
@logo.falsify_all_others
flash[:notice] = "Logo has been changed successfully!"
redirect_to logos_path
else
flash[:alert] = "Logo has not been updated."
render :action => "edit"
end
end
def show
@logo = Logo.find(params[:id])
end
def index
@logos = Logo.all
end
end
和我的应用程序控制器
应用程序/控制器/application_controller.rb
before_filter :get_logo
def get_logo
@logo_default = Logo.default
end
因此,一旦他们通过carrierwave创建/上传了一些图像,我就会像这样显示徽标:app/views/logos/show.html.erb
<%= image_tag @logo.image_url.to_s %>
我想添加默认徽标(通过我的徽标范围找到)以显示在主页上。现在,它是通过此代码通过 app/assets/stylesheets/styles.scss.erb 完成的
section#header {
height: 220px;
position: relative;
h1 {
background-image: url('<%= asset_path 'logo.png' %>');
position: absolute;
width: 412px;
height: 188px;
z-index: 999;
}
所以我的问题是:我是否需要将显示徽标的代码从 scss.erb 中提取并添加到 app/views/layouts/application.html.erb,或者我可以在 scss 中更改它吗?比如用 image_tag 替换asset_path,或者类似的东西?如果您需要更多信息/代码,请告诉我,我很乐意提供...提前致谢