エラーが発生しているようです:
uninitialized constant Style::Pic
ネストされたオブジェクトを index ビューにレンダリングしようとすると、show ビューは問題ありません。
class Style < ActiveRecord::Base
#belongs_to :users
has_many :style_images, :dependent => :destroy
accepts_nested_attributes_for :style_images,
:reject_if => proc { |a| a.all? { |k, v| v.blank?} } #found this here http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
has_one :cover, :class_name => "Pic", :order => "updated_at DESC"
accepts_nested_attributes_for :cover
end
class StyleImage < ActiveRecord::Base
belongs_to :style
#belongs_to :style_as_cover, :class_name => "Style", :foreign_key => "style_id"
has_attached_file :pic,
:styles => { :small => "200x0>",
:normal => "600x> " }
validates_attachment_presence :pic
#validates_attachment_size :pic, :less_than => 5.megabytes
end
<% for style_image in @style.style_images %>
<li><%= style_image.caption %></li>
<div id="show_photo">
<%= image_tag style_image.pic.url(:normal) %></div>
<% end %>
上記からわかるように、メイン モデル スタイルには多くの style_images があり、これらすべての style_images は show ビューに表示されますが、index ビューでは、名前が付けられ、表示されるカバーとして機能する 1 つの画像を表示したいと考えています。スタイルごとに。
インデックスコントローラーで、次のことを試しました:
class StylesController < ApplicationController
layout "mini"
def index
@styles = Style.find(:all,
:inculde => [:cover,]).reverse
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @styles }
end
end
そしてインデックス
<% @styles.each do |style| %>
<%=image_tag style.cover.pic.url(:small) %>
<% end %>
class StyleImage < ActiveRecord::Base
belongs_to :style
#belongs_to :style_as_cover, :class_name => "Style", :foreign_key => "style_id"
has_attached_file :pic,
:styles => { :small => "200x0>",
:normal => "600x> " }
validates_attachment_presence :pic
#validates_attachment_size :pic, :less_than => 5.megabytes
end
style_images テーブルには、cover_id もあります。
コントローラとモデルにカバーが含まれていることがわかります。私はここでどこが間違っているのか知っています!
誰かが助けることができるなら、してください!