0

我有一个部分,其中有一个实例变量。我试图消除这个实例变量,因为实例变量不应该是部分的。当我尝试消除它时,我遇到了错误

带有实例变量的部分

<%= 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 为零

还尝试创建一个 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 应该是什么

想法模型:类想法 < ActiveRecord::Base

 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} %>
4

1 回答 1

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%> 
于 2013-11-08T16:06:09.227 回答