我正在努力使我的rails应用程序的徽标可以互换,请参阅上一篇文章here
我有一个Logo模型,其中包含以下字段名称:string,image:string,default:boolean
我正在使用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
我有一个徽标控制器,见这里
应用程序/控制器/ 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,或类似的东西?如果您需要更多信息/代码,请告诉我,我很乐意提供...提前致谢
答案 0 :(得分:0)
我建议不要把它放在scss中,因为你可能想要在生产中预先编译所有这些静态的东西。