I am putting together an app that allows users to make posts. Each post will have a type (image, video, link, text) with both similar and unique variables. I want to submit these through different forms in a "composer" that's available throughout the site.
Here is the post model, Gallery:
class Gallery < ActiveRecord::Base
attr_accessible :name, :asset
has_attached_file :asset, :styles => { :small => '160x120#', :medium => "500x", :thumb => "300x200#" }
belongs_to :user
has_many :likes
validates :user_id, presence: true
validates :type, presence: true
validates :name, presence: true, length: { maximum: 140, minimum: 1 }
end
I was thinking of using Single Table Inheritance, like this:
class Image < Gallery
end
class Link < Gallery
end
class Video < Gallery
end
class Text < Gallery
end
But I'm not achieving the result I want. For one, I'd like to use the methods in my Gallery controller, such as:
# galleries_controller.rb
def like
@gallery = Gallery.find(params[:id])
@like = @gallery.likes.build(:user_id => current_user.id)
respond_to do |format|
if @like.save
format.html { redirect_to @gallery }
format.js
end
end
end
Moreover, I want to create a "publisher" form that contains a form for each post type and lets you create any kind of post.
How would you approach this? I'm new to Rails and I want to take advantage of all the conveniences it offers. Much appreciated!