我有一个部分,其中有一个实例变量。我试图消除此实例变量,因为实例变量不应该在partials中。当我尝试消除它时,我遇到了错误
部分使用实例变量
<%= f.label :file, "<span style='background-color: ##{@idea.first_color_hex}' >#
{image_tag(f.object.file.url(:icon))}</span>".html_safe %>
部分没有实例变量
<%= f.label :file, "<span style='background-color: ##{idea.first_color_hex}' >#
{image_tag(f.object.file.url(:icon))}</span>".html_safe %>
获得想法
我尝试了什么:
将此变量创建为对象
<%= f.label :file, "<span style='background-color: ##{f.object.idea.first_color_hex}' >#
{image_tag(f.object.file.url(:icon))}</span>".html_safe %>
为first_color_hex
获取nil还尝试创建if then语句
<% if f.object.idea.nil? %>
<%= f.label :file, "<span style='background-color: ##{@idea.first_color_hex}' >#
{image_tag(f.object.file.url(:icon))}</span>".html_safe %>
<%else%>
<%= f.label :file, "<span style='background-color: ##{f.object.idea.first_color_hex}'
>#{image_tag(f.object.file.url(:icon))}</span>".html_safe %>
<%end%>
但是那里仍然有一个实例变量。不确定if应该是什么
创意模型: class Idea&lt;的ActiveRecord ::基
attr_accessor :product_line_tokens
has_many :artworks, -> { order('dimensions ASC') }
has_and_belongs_to_many :colors, -> { order('name asc').uniq }
has_and_belongs_to_many :imprintables, -> { uniq }, :join_table => :imprintables_ideas
has_and_belongs_to_many :stores, -> { order('name asc').uniq }, :join_table =>
:stores_ideas
has_and_belongs_to_many :taxonomies, -> { order('name asc').order('store_id').uniq },
:join_table => :taxonomies_ideas
has_and_belongs_to_many :product_lines, :join_table => "product_lines_ideas"
belongs_to :default_artwork, :class_name => "Artwork", :foreign_key =>
:default_artwork_id
belongs_to :copywriter, :class_name => 'User', :foreign_key => :copywriter_id
belongs_to :artist, :class_name => 'User', :foreign_key => :artist_id
has_many :mockups
def first_color_hex
if colors.count == 0
return nil
else
return colors[0].hex_code
end
end
部分标签:
<%= render :partial => 'art_show', :locals => {:idea => @idea} %>
答案 0 :(得分:0)
我明白了。
我用if else
删除了实例变量<% if f.object.idea.nil? %>
<%= f.label :file, "<span style='background-color: none' >#
{image_tag(f.object.file.url(:icon))}</span>".html_safe %><br />
<%else%>
<%= f.label :file, "<span style='background-color: ##
{f.object.idea.first_color_hex}' >#{image_tag(f.object.file.url(:icon))}</span>".html_safe
%><br />
<%end%>