0

我正在尝试为我的网络应用程序创建自己的简单多文件上传功能。但是,当用户提交他的图像时,我无法弄清楚如何循环浏览表中某一行中发布的条目。

发布的数据

{"album_id"=>"10",
 "utf8"=>"✓",
 "commit"=>"Create Image",
 "authenticity_token"=>"O9/iRhRJozks9ZE/LXfbDDZQa2/vEFcigu7Edkn3bWY=",
 "image"=>{"album_id"=>"10",
 "description"=>"Example",
 "albumcover"=>"false",
 "title"=>"Title",
 "picture"=>[#<ActionDispatch::Http::UploadedFile:0x10255fc28 @tempfile=#<File:/var/folders/bx/6z1z5yks56j40v15n43tjh1c0000gn/T/RackMultipart20130325-6919-14738ar-0>,
 @headers="Content-Disposition: form-data; name=\"image[picture][]\"; filename=\"IMG_0106.JPG\"\r\nContent-Type: image/jpeg\r\n",
 @content_type="image/jpeg",
 @original_filename="IMG_0106.JPG">,
 #<ActionDispatch::Http::UploadedFile:0x10255fc00 @tempfile=#<File:/var/folders/bx/6z1z5yks56j40v15n43tjh1c0000gn/T/RackMultipart20130325-6919-1o8msmh-0>,
 @headers="Content-Disposition: form-data; name=\"image[picture][]\"; filename=\"Screen Shot 2013-03-05 at 9.00.28 PM.png\"\r\nContent-Type: image/png\r\n",
 @content_type="image/png",
 @original_filename="Screen Shot 2013-03-05 at 9.00.28 PM.png">],
 "image_id"=>"id"}}

如您所见,该:picture行中有两个不同的文件数据。如何循环浏览该数据以将每个数据分配给不同的图像对象?

架构

ActiveRecord::Schema.define(:version => 20130319040930) do

  create_table "albums", :force => true do |t|
    t.string   "title",       :null => false
    t.text     "description"
    t.date     "date"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
    t.integer  "album_id"
  end

  create_table "images", :force => true do |t|
    t.string   "title"
    t.string   "description"
    t.datetime "date"
    t.integer  "album_id"
    t.integer  "image_id"
    t.string   "picture"
    t.boolean  "albumcover"
  end

  create_table "users", :force => true do |t|
    t.string   "email"
    t.string   "password_digest"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
  end

end

图像模型

class Image < ActiveRecord::Base
    attr_accessible :title, :description, :picture, :image_id, :album_id, :albumcover
    belongs_to :album
    accepts_nested_attributes_for :album
    mount_uploader :picture, PictureUploader 
  end

图像控制器

class Admin::ImagesController < ApplicationController
    respond_to :html, :json
    def index
        @album = Album.find(params[:album_id])
        @images = @album.images.all
    end
    # def new
    #   @album = Album.find(params[:album_id])
    #   @image = @album.images.new
    # end
    def new
        @album = Album.find(params[:album_id])
            @image = @album.images.new
    end
    def create
        @album = Album.find(params[:album_id])
        @image = @album.images.new(params[:image])
        if @image.save
            flash[:notice] = "Successfully added image!"
            redirect_to [:admin, @album, :images]
        else
            render :action => 'new'
        end
    end
    def show
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
    end
    def edit
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
    end
    def update
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
        if @image.update_attributes(params[:image])
            flash[:notice] = "Successfully updated Image"
            redirect_to [:admin, @album, :images]
        else
            render :action => "edit"
        end
    end
    def destroy
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
        @image.destroy
        @albumid = @album.id
        @id = @image.id
        FileUtils.remove_dir("#{Rails.root}/public/uploads/image/picture/#{@albumid}/#{@id}", :force => true)
        redirect_to admin_album_images_path(@album)
    end

end

看法

<%= stylesheet_link_tag 'admin/dashboard' %>
<%= stylesheet_link_tag 'admin/images' %>
<%= javascript_include_tag 'admin/dashboard'%>
<div class="container">
    <header class="header">
            <div class="header_content clearfix">
                <h1 class="page_name">Dashboard</h1>
                    <ul class="navbar">
                        <li class="nav_item">Move</li>
                        <li class="nav_item">Edit</li>
                        <li class="nav_item">Delete</li>
                    </ul>
            </div>
    </header>
    <section class="sidebar">
        <div class="sidebar_content">
            <ul class="menu">
                <li class="menu_item active-menu"><%=link_to 'manage albums', admin_albums_path%></li>
                <li class="menu_item"><%=link_to 'manage media' %></li>
                <li class="menu_item"><%=link_to "New Album", :controller => "albums", :action => "new" %></li>
                <li class="menu_item"><%= link_to "Add Images", {:controller => "images", :action => "new"}, :class => "highlight_menu"%> </li>
                <li class="menu_item"></li>
            </ul>
        </div>
    </section>
    <section class="content">
        <div class="inner-box clearfix">
            <%= link_to "< Back", admin_albums_path, :id => "return_link" %> </br>
            <h1 class="section-title"> <strong style="font-weight: 600;"><%=best_in_place [:admin,@album], :title %></strong></h1>
            <h4 class="album-desc"><%= best_in_place [:admin,@album], :description, :type => :textarea%></h4>
            <% if !@images.blank? %>
                <% @images.each do |image| %>
                <div class="item">
                    <div class="image-box">
                        <div class="source">
                            <%= image_tag image.picture %>
                        </div>
                    </div>
                    <div class="info">
                        <div class="title">
                            <%= image.title %>
                        </div>
                        <div class="item-links">
                            <%= link_to "Edit", edit_admin_album_image_path(@album, image.id)%>
                            <%= link_to "Delete", 
                                admin_album_image_path(@album, image.id),
                                :class => "item-link delete-image",
                                :method => :delete, 
                                :remote => true,
                                :confirm => "Are you sure?" %>
                        </div>  
                        <div class="description clearfix">
                            <p>
                                <%= truncate(image.description, :length => 40) %>
                            </p>
                        </div>
                        <%= label(:albumcover, 'Set as Album Cover ', :style => "float: left;") %>
                        <%= radio_button_tag(:albumcover, :albumcover, false) %>
                    </div>
                </div>
                <% end %>
            <% else %>
                <p class="alert">No images in this album</p>
            <% end %>
        </div>
    </section>
</div>
4

0 回答 0