2013年2月3日日曜日

rails3 複数ファイルのアップロード

ポイントはinput にmultiple属性の追加とinput のtype="file"でファイルを選択終了するとjavascriptで新しいinput type="file"を作るです。サンプルはこちら
新規作成中の画面
編集中の画面









ファイル選択ボタンを押す毎に新しいファイル選択ボタンの作成をしています
 
新規作成用
<%= form_for(@book,:html => {:multipart => true}) do |f| %>
  <% if @book.errors.any? %>
    

<%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:

    <% @book.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= hidden_field_tag("imagenum",'',{:class => :imagenum}) %>
<%= f.submit %>
<% end %>
編集用
<%= form_for(@book,:html => {:multipart => true}) do |f| %>
    <% if @book.errors.any? %>
        

<%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:

    <% @book.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %>
<%= f.label :title %>
<%= f.text_field :title %>
<% @book.images.each do |image| %> <%= check_box_tag "images[]",image.id,true %><%= image.filename %> <% end %> <%= hidden_field_tag("imagenum",'',{:class => :imagenum}) %>
<%= f.submit %>
<% end %>
これではわかりにくいと思うのでgithubにサンプルをアップしました localhost:3000/booksあたりからアクセスしてください

0 件のコメント:

コメントを投稿