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

アイデアの nil を取得する

私が試したこと:

この変数をオブジェクトとして作成する

<%= 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 < 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 に答える