私はRails開発にとても慣れていません。ポートフォリオサイト用のシンプルなバックエンドを作成しています。
この質問のタイトルがわかりません。私が尋ねた前の質問は多分複雑すぎます。だから私はそれを単純化しています。
3つのモデルを使用しています:Post、Attachment、Attachment_Category
私は私が使用するフォームを持っています:
タイトル、コンテンツ、カテゴリを使用して投稿の下書きを作成します。
ドロップダウンに添付ファイルのカテゴリを表示する(スライドショー、画像、ビデオ)
添付ファイルをアップロードします。
手順1と2を実装しました。
ステップ3の場合:フォームで最終的に送信を押したときに、attachment_category_idが添付ファイルテーブルに保存されるようにします。
私には次の関係があります。
Post.rb
class Post < ActiveRecord::Base
has_many :attachment_categories, :through => :attachments
has_many :attachments,:dependent => :destroy
accepts_nested_attributes_for :attachments
validates_presence_of :title, :content, :category
end
Attachment.rb
class Attachment < ActiveRecord::Base
belongs_to :post
belongs_to :attachment_category
#paperclip
has_attached_file :photo, :styles =>{
:thumb => "100x100#",
:small => "400x400>"
}
end
Attachment_category.rb
class AttachmentCategory < ActiveRecord::Base
has_many :posts , :through => :attachments
has_many :attachments
validates :category_name, :presence =>true
end