Controller code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  def new
    @project = Project.new
    1.upto(1) { @project.photos.build }
    @project_category = ProjectCategory.all
  end

  def create
    @project_category = ProjectCategory.all
    @project = Project.new(params[:project])
    if @project.save
      flash[:notice] = "Successfully created project."
      redirect_to @project
    else
      render :action => 'new'
    end
  end

  def edit
    @project = Project.find(params[:id])
    @project_category = ProjectCategory.all
    if @project.photos.first.nil?
      1.upto(1) { @project.photos.build }
    end

  end

  def update
    @project = Project.find(params[:id])

    if @project.update_attributes(params[:project])
      flash[:notice] = "Successfully updated project."
      redirect_to @project
    else
      render :action => 'edit'
    end
  end

form partial

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<% form_for @project, :html => {:multipart => true} do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :projectName %><br />
    <%= f.text_field :projectName %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description, :rows => 2 %>
  </p>

    <%= f.label :category_id, "Type"%><br />
  <% @project_category.each do |cat|%>
    <%= f.radio_button :project_category_id, cat.id %>
    <%= cat.id%>
    <%= cat.title %>
  <% end %>
<div class="album_photos">
  <%= render :partial => 'project_photo', :collection => @project.photos %>
</div>

<div id="photos">
  <%= render :partial => 'photo', :collection => @project.photos %>
</div>
<%= link_to_function "Add Photo" do |page|
  page.insert_html :bottom, :photos, :partial => 'photo', :object => Photo.new
end %>
  <p><%= f.submit "Submit" %></p>
<% end %>

project_photo

1
2
3
4
<% unless project_photo.new_record? %>
  <%= image_tag(project_photo.data.url(:thumb), :alt => project_photo.description) %>
  <%= check_box_tag "photo_ids[]", project_photo.id %>
<% end rescue nil %>

photo partial

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<div class="photo">
<fieldset>
<% fields_for "project[photo_attributes][]", photo do |p| %>
  <p>
    <%= p.label :Photo %><br />
    <%= p.file_field :data, :index => nil %>
    <%= link_to_function "delete", "remove_field($(this), ('.photo'))" %>
  </p>
  <p>
  <%= p.text_field :description
  </p>

<% end %>
</fieldset>
</div>

project model

1
2
3
4
5
6
7
8
9
10
class Project < ActiveRecord::Base
  has_many :photos
  has_one :project_category

  def photo_attributes=(photo_attributes)
    photo_attributes.each do |attributes|
      photos.build(attributes)
    end
  end
end

photo model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Photo < ActiveRecord::Base
 attr_accessible :id, :description, :photo
 belongs_to :project

  has_attached_file :data,
  :styles => {
    :thumb => "150x150#",
    :large => "640x480#",
    :fullsize => "800x600#"
  }

  validates_attachment_size :data, :less_than => 2.megabytes

  #validates_attachment_presence :data
  validates_attachment_content_type :data, :content_type => ['image/jpeg','image/jpg', 'image/png']

end

the photo migration file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class CreatePhotos < ActiveRecord::Migration
  def self.up
    create_table :photos do |t|
      t.references :project
      t.string :description
      t.timestamps
    end
    add_index :photos, :project_id
  end

  def self.down
    drop_table :photos
  end
end