fields_for を含むフォームでネストされたフィールドを使用しています。複数の添付ファイルを動的に追加してフォームを送信する必要があります。コンソールでは、添付ファイルのパラメーターを次のように取得しています
"reminder"=>{"message_addl_attachments_attributes"=>{"1380913359931"=>{"attachment"=>#<File:/tmp/RackMultipart20131005-4343-1cxemfz-0>}}
コントローラーでこれを処理して添付ファイルを保存する方法がわかりません。関連付けを適切に構築しました。コントローラーでは、このようにしています。
def create_reminder
@reminder = Reminder.new
@addl = @reminder.message_addl_attachments.build(params[:reminder][:message_addl_attachments])
@addl.save
end
コンソールで完全なパラメータを見つけてください。
Processing ReminderPluginController#create_reminder (for 127.0.0.1 at 2013-10-05 00:53:33) [POST]
Parameters: {"reminder"=>{"subject"=>"nkljkl", "body"=>"<p>jkljkljkl jlkj</p>", "email"=>"", "message_addl_attachments_attributes"=>{"1380914609923"=>{"attachment"=>#<File:/tmp/RackMultipart20131005-4488-1agtr7m-0>}}}, "controller"=>"reminder_plugin", "select_batch"=>{"batch"=>"2"}, "select_department"=>{"department"=>""}, "recipients"=>"20", "authenticity_token"=>"L0py7Xdsf7JSQGqn2bdoE7EXDVN9ZmNNSwl0dbHMkfM=", "action"=>"create_reminder", "commit"=>"Send"}
[paperclip] Saving attachments.
Redirected to http://localhost:3000/reminder/create_reminder
Completed in 459ms (DB: 217) | 302 Found [http://localhost/reminder_plugin/create_reminder]
しかし、それは私のレールのパブリックディレクトリに保存されていません...データベースではnull値も保存されています..助けてください。
============== myview.html.erb
<% form_for @reminder, :url => { :action => "create_reminder"
},:html=>{:multipart=>true} do |l| %>
<div class="addl_attachments">
<% l.fields_for :attachments do |a| %>
<%= render "message_addl_attachment_fields",:f=>a %>
<% end %>
<div class="add_addl_attachment">
<%= link_to_add_addl_attachment "#{image_tag
"buttons/add_2.png" } #{t('add_txt')}", l, :attachments %>
</div>
</div>
</div>
<div id="submit-button">
<%=submit_tag "#{t('send')}", :class => 'button',
:disable_with => "#{t('please_wait')}" %>
</div>
<% end %>