ファイルのアップロードにポリモーフィックな関連付けを設定しようとしています。現在、以下のコードで保留中ですが、2 つの問題に非常に長い時間がかかります。いろいろ調べたのですが、解決策が見つかりません
#post.rb
class Post < ActiveRecord::Base
attr_accessible :content,:tags_attributes, :attachments_attributes
has_many :tags
has_many :attachments, :as => :attachable
accepts_nested_attributes_for :attachments
end
#attachment.rb
class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
attr_accessible :description, :file
mount_uploader :file, FileUploader
end
# _form.html.erb
<%= form_for @post , :html=>{:multipart => true } do |post_form| %>
<%= post_form.fields_for :attachments do |attachment_form| %>
<p>hello</p>
<p>
<%= attachment_form.label :description %><br />
<%= attachment_form.text_area :description %>
</p>
<p>
<%= attachment_form.label :file %><br />
<%= attachment_form.file_field :file %>
</p>
<% end %>
<%= post_form.submit %>
<% end %>
ページにはボタンしか表示されませんが、フォーム部分はどこにありますか? ここでのfields_forの使い方は正しいと思います。以前にタグモデルを定義したことがあるからです。これは、アクセスするメカニズムとほとんど同じです。<%= post_form.fields_for :tags do |tag_form|の形式を使用します %>、しかし、なぜポリモーフィックになると何も表示されないのですか??
問題 1 の一時的な修正として、fields_forステートメントを以下のように更新しました。フォームがページに表示されるようになりましたが、送信ボタンを押すと、保護された属性を一括割り当てできません: 添付ファイルという問題が表示されます。
<%= post_form.fields_for @post.attachments.build do |attachment_form| %>
これら2つの問題を解決するのを手伝ってくれる人はいますか?