Powered by SmartDoc

4.3.3 check_images によるイメージ管理

  1. 現在のイメージを確認(app/controllers/hujiminekos_controller.rb)

    @@ -24,6 +24,8 @@
     
       # GET /hujiminekos/1/edit
       def edit
    +    check_images(:edit)
    +    @documents = Document.all
       end

  2. データの入力(app/views/hujiminekos/_form.html.erb)

    @@ -71,6 +71,33 @@
         <%= form.check_box :solved %>
       </div>
     
    +  <div class="field">
    +    <%= form.label t('individual.delete_image') %>
    +    <ul>
    +      <% hujimineko.images.each do |image| %>
    +      <%   document = @documents.where(blob_id: image.blob_id).first %>
    +      <%   if not document.nil? %>
    +      <%     comment = document.content + '(' + image.blob_id.to_s + '): ' %>
    +      <%   else %>
    +      <%     comment = '(' + image.blob_id.to_s + '): ' %>
    +      <%   end %>
    +      <li>
    +      <%=  hidden_field_tag "image_checked[#{image.blob_id}]", false %>
    +      <%=  check_box_tag "image_checked[#{image.blob_id}]", true, false, {} %>
    +      <%=  label_tag "image_checked[#{image.blob_id}]", comment %>
    +      <%=  image_tag image.representation(resize_to_limit: [100, 100]) %>
    +      </li>
    +      <% end %>
    +    </ul>
    +    <%= form.label t('individual.image_content') %>
    +    <%= form.text_field :image_content %>
    +    <%= form.label t('individual.upload') %>
    +    <%  hujimineko.images.each do |image| %>
    +    <%=   form.hidden_field :images, multiple: true, value: image.signed_id %>
    +    <%  end %>
    +    <%= form.file_field :images, multiple: true %>
    +  </div>
    +
       <div class="actions">
         <%= form.submit %>
       </div>

  3. チェックボックスでの渡し方
  4. データの更新(update)・表示(show)・削除(destroy)の処理(app/controllers/hujiminekos_controller.rb)

    @@ -15,6 +15,7 @@
     
       # GET /hujiminekos/1 or /hujiminekos/1.json
       def show
    +    @documents = Document.all
       end
     
       # GET /hujiminekos/new
    @@ -73,6 +74,7 @@
               end
               user.save
             end
    +        check_images(:update, params['image_checked'])
             format.html { redirect_to hujimineko_url(@hujimineko), notice: t('individual.notice.updated', name: $individual[:name], id: $individual[:id]) }
             format.json { render :show, status: :ok, location: @hujimineko }
           else
    @@ -94,6 +96,7 @@
           user.alert = true
         end
         user.save
    +    check_images(:destroy)
         @hujimineko.destroy
     
         respond_to do |format|

  5. 結果の表示(app/views/hujiminekos/show.html.erb)

    @@ -62,5 +62,25 @@
       <%= @hujimineko.solved %>
     </p>
     
    +<ul>
    +  <% @hujimineko.images.each do |image| %>
    +    <li>
    +      <% if image.representable? %>
    +      <%   document = @documents.where(blob_id: image.blob_id).first %>
    +      <%   if not document.nil? %>
    +      <%=    document.content + '(' + image.blob_id.to_s + '): ' %>
    +      <%   else %>
    +      <%=    '(' + image.blob_id.to_s + '): ' %>
    +      <%   end %>
    +      <%=  image_tag image.representation(resize_to_limit: [100, 100]) %>
    +      <% else %>
    +      <%=  link_to rails_blob_path(image, disposition: "attachment") do %>
    +      <%=  image_tag "placeholder.png", alt: "Download image" %>
    +      <% end %>
    +      <% end %>
    +    </li>
    +  <% end %>
    +</ul>
    +
     <%= link_to t('page.instruction.edit'), edit_hujimineko_path(@hujimineko) %> |
     <%= link_to t('page.instruction.back'), hujiminekos_path %>