Nested attributes in the index view?

Posted by user283179 on Stack Overflow See other posts from Stack Overflow or by user283179
Published on 2010-03-15T14:22:48Z Indexed on 2010/03/15 16:19 UTC
Read the original article Hit count: 494

I seem to be getting error:

uninitialized constant Style::Pic

when I'm trying to render a nested object in to the index view the show view is fine.

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

As you can see from the above The main model style has many style_images, all these style_images are displayed in the show view but, in the the index view I wish to show one image which has been name and will act as a cover that is displayed for each style.

in the index controller I have tried the following:

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

and the index

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

In the style_images table there is an cover_id also.

From the about you can see that I have included the cover in the controller and the model. I have know idea where I'm going wrong here!

If any one can help please do!

© Stack Overflow or respective owner

Nested attributes in the index view?

Posted by user283179 on Stack Overflow See other posts from Stack Overflow or by user283179
Published on 2010-03-15T16:05:01Z Indexed on 2010/03/15 16:19 UTC
Read the original article Hit count: 494

How would I show one of many nested objects in the index view

class Album < ActiveRecord::Base 

has_many: photos
accepts_nested_attributes_for :photos,
 :reject_if => proc { |a| a.all? { |k, v| v.blank?} }


has_one: cover
accepts_nested_attributes_for :cover


end

class Album Controller < ApplicationController
  layout "mini"
  def index
    @albums = Album.find(:all,
    :include => [:cover,]).reverse

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @albums }
    end
  end

This is what I have so fare. I just want to show a cover for each album.

Any info on this would be a massive help!!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails