I am new to Ruby on Rails. I am trying to upload multiple images using paperclip. I have three model * Award * Album * Photo
I have the following mappings
In model Award i have
has_one :album
In the model Album i have
has_many :photos
belongs_to :award
In the model photo i have
belongs_to :album
so when i create a new award, it will have an album, and the album will have many photos. I am using paperclip in model photo.
so when i create a new award how will i link the album and photo, and how i am suppose to save the photos. How to handle multiple image uploading using paperclip in this case?
Award model
class Award < ActiveRecord::Base
attr_accessible :album_id, :award_title, :description, :receiving_date
#mappings
has_one :album
#validate presence of fields
validates_presence_of :album_id,:title,:description,:receiving_date
#validate that description has minimum length 3
validates_length_of :description, :minimum => 3
#validates that title has length >=3
validates_length_of :title, :minimum => 3
end
Album model
class Album < ActiveRecord::Base
attr_accessible :description, :title, :award_id
#mappings
has_many :photos
belongs_to :award
#validate presence of fields
validates_presence_of :award_id,:title,:description
#validate that title has minimum length 3
validates_length_of :title, :minimum => 3
#validate that description has minimum length 10
validates_length_of :description, :minimum => 10
end
Photo model
class Photo < ActiveRecord::Base
attr_accessible :album_id, :image_url
#mappings
belongs_to :album
#validate presence of fields
validates_presence_of :album_id,:image_url
end
You may wish to look at the has_many :through
relationship - which means you use one model (album in your case) as a join model
, which will have two foreign keys - photo_id
and award_id
Here's what I'd do:
#app/models/album.rb
Class Album < ActiveRecord::Base
belongs_to :photo, :class_name => 'Photo'
belongs_to :award, :class_name => 'Award'
accepts_nested_attributes_for :photo
end
#app/models/award.rb
Class Award < ActiveRecord::Base
has_many :albums, :class_name => 'Album'
has_many :photos, :class_name => 'Photo', :through => :albums
accepts_nested_attributes_for :albums
end
#app/models/photo.rb
Class Photo < ActiveRecord::Base
has_many :albums, :class_name => 'Album'
has_many :awards, :class_name => 'Award', :through => :albums
end
This will mean that you'll be able to call @award.photos
-- it will show you all the photos which are contained in the albums assigned to the award
To upload multiple images to this, you'd be able to use accepts_nested_attributes_for
like this:
#app/controllers/awards_controller.rb
def new
@award = Award.new
@award.albums.build.build_photo
end
def create
@award = Award.new(award_params)
@award.save
end
private
def award_params
params.require(:award).permit(:award, :variables, albums_attributes: [:extra, :vars, photo_attributes: [:photo]])
end
You could then add this to your upload form:
<%= form_for @award do |f| %>
<%= f.text_field :your_vars %>
<%= f.fields_for :albums do |a| %>
<%= a.text_field :extra_var %>
<%= a.fields_for :photo do |p| %>
<%= p.file_field :photo %>
<% end %>
<% end %>
<% end %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.