Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 索引视图中的嵌套属性?_Ruby On Rails_Ruby_Nested Attributes - Fatal编程技术网

Ruby on rails 索引视图中的嵌套属性?

Ruby on rails 索引视图中的嵌套属性?,ruby-on-rails,ruby,nested-attributes,Ruby On Rails,Ruby,Nested Attributes,我似乎犯了一个错误: uninitialized constant Style::Pic 当我试图在索引视图中渲染嵌套对象时,显示视图是可以的 class Style < ActiveRecord::Base #belongs_to :users has_many :style_images, :dependent => :destroy accepts_nested_attributes_for :style_images, :reject_if => proc { |a

我似乎犯了一个错误:

uninitialized constant Style::Pic
当我试图在索引视图中渲染嵌套对象时,显示视图是可以的

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 %>
从上面可以看到,主模型样式有许多样式图像,所有这些样式图像都显示在“显示”视图中,但在“索引”视图中,我希望显示一个已命名的图像,作为每个样式显示的封面

在索引控制器中,我尝试了以下操作:

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
在“样式图像”表中,还有一个封面id

从关于你可以看到,我已经包括在控制器和模型的封面。 我知道我哪里出了问题

如果有人能帮忙,请做

您必须修复:cover关联定义,如下所示

has_one :cover, :class_name => "StyleImage", :order => "updated_at DESC"
我在你的设计中发现了另一个潜在的问题。您有一个has\u many和一个has\u one关联,使用相同的外键样式\u id指向同一个表

将has_one关联更改为belishing_to,并将外部keystyle_image_id存储在样式表中,即

class Style < ActiveRecord::Base
  has_many :style_images
  belongs_to  :cover, :class_name => "StyleImage"
end

Pic位于数据库的style_images表中。
has_one  :cover, :class_name => "StyleImage", :conditions => {:itype => "cover"}
class Style < ActiveRecord::Base
  has_many :style_images
  belongs_to  :cover, :class_name => "StyleImage"
end