-3

このハッシュリクエストを2つの異なる文字列に分割するにはどうすればよいですか?#<ActionDispatchリクエストで選択された次の画像の開始を示すで分割したいと思います。ルビーでこれを達成するにはどうすればよいですか?

リクエスト

{"image"=>{"picture"=>[#<ActionDispatch::Http::UploadedFile:0x10c986d88 @tempfile=#<File:/var/folders/bx/6z1z5yks56j40v15n43tjh1c0000gn/T/RackMultipart20130404-53101-3c2whv-0>,
 @headers="Content-Disposition: form-data; name=\"image[picture][]\"; filename=\"background-pic.jpg\"\r\nContent-Type: image/jpeg\r\n",
 @content_type="image/jpeg",
 @original_filename="background-pic.jpg">,
 #<ActionDispatch::Http::UploadedFile:0x10c986d60 @tempfile=#<File:/var/folders/bx/6z1z5yks56j40v15n43tjh1c0000gn/T/RackMultipart20130404-53101-bvdysw-0>,
 @headers="Content-Disposition: form-data; name=\"image[picture][]\"; filename=\"bible-banner.png\"\r\nContent-Type: image/png\r\n",
 @content_type="image/png",
 @original_filename="bible-banner.png">],
 "album_id"=>"10"},
 "authenticity_token"=>"dr8GMCZOQo4dQKgkM4On2uMs8iORQ68vokjW0e4VvLY=",
 "commit"=>"Submit",
 "utf8"=>"✓",
 "album_id"=>"10"}

コントローラ

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
        #@image.picture.size.times {@image.build}
    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 '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 "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

モデル

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

フォームビュー

<%= form_for([:admin, :album, @image], :html => {:multipart => true}) do |f| %>
  <%= f.hidden_field :album_id, :value => @album.id %>
  <%= f.file_field :picture, :multiple => true %>
  <%= f.submit "Submit" %>
<%end%>

アップローダーCarrierwave

class PictureUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  # include Sprockets::Helpers::RailsHelper
  # include Sprockets::Helpers::IsolatedHelper

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.album_id}/#{model.id}"
  end
end
4

3 に答える 3

3

数人のコメント投稿者が上で述べたように、あなたが持っているのはpicture、値がすでに配列であるキーを持つハッシュ(キー/値のペア)であり、その性質上、個別のオブジェクトに分割されます。

配列にアクセスするには、キーを要求する必要があります。次に、 Arrayメソッドを使用してそれぞれにpictureアクセスできます。UploadedFile

したがって、最初のハッシュから始めます。

hash_with_stuff = { "picture" => [#<UploadedFile1>,#<UploadedFile2>]}
  #=> { "picture" => [#<UploadedFile1>,#<UploadedFile2>]}

配列を取得するには:

array_of_pictures = hash_with_stuff["picture"]
  #=> [#<UploadedFile1>,#<UploadedFile2>]

配列ができたので、他の配列と同じように操作できます。

# to get single elements out
array_of_pictures[0] # where the number is the index of the object in the array (starting with 0)
  #=> #<UploadedFile1>   #this is an UploadedFile object and will respond to its own methods

# to operate on each element with some method
array_of_pictures.each do |picture|
  picture.create_thumbnail
end

これは理にかなっていますか、それともこれを扱うことについて他の質問がありましたか?

于 2013-04-04T18:30:25.343 に答える
0

このハッシュh={"strings" => ["stirng_object_1"、 "string_object_2"]}を見てみましょう。これは、配列内のオブジェクトがActionDispatch :: Httpではなく文字列であることを除いて、投稿したハッシュとほぼ同じです。 ::UploadedFileオブジェクト。

したがって、最初の文字列オブジェクトを取得する必要がある場合に引用したハッシュの例から、

h ["string"] [0]

同様に、最初のActionDispatch :: Http :: UploadedFileオブジェクトにアクセスする場合は、実行できます(ハッシュ名がhであると想定)。

h ["picture"] [0]

于 2013-04-04T18:36:36.427 に答える
0

このハッシュリクエストを個別の文字列に分割する方法を理解しました。

イメージコントローラー

def new
    @album = Album.find(params[:album_id])
    @image = @album.images.new
    end
    def create
        params[:image][:picture].each do |image|
            @album = Album.find(params[:album_id])
            @params = {}
            @params['picture'] = image
            @image = @album.images.create(@params)
       end
        if @image.save
            if params[:image][:picture].size > 1
                flash[:notice] = "Successfully added images!"
            else
                flash[:notice] = "Successfully added image!"
            end
            redirect_to [:admin, @album, :images]
        else
            render "new"
            flash[:notice] = "Did not successfully add image :("
        end
    end
于 2013-04-11T14:04:46.267 に答える