mercredi 16 août 2017

How to upload multiple images with Rails 3 + mongoid + carrierwave-mongoid?

I`m newbie in Rails. I try to create form for saving and editing posts (text title, content and images). There are my model for post:

class Post
  include Mongoid::Document
  include Mongoid::Timestamps
  field :title, type: String
  field :content, type: String
  field :image
  field :attachments # - field for files (images) array

  mount_uploader :image, ImageUploader
  mount_uploader :attachments, ImageUploader # - uploader for multiple files
end

It`s my Post controller (works fine for single image upload):

class PostsController < ApplicationController

  def index
    @posts = Post.all
  end

  def create
    p params
    @post = Post.new(posts_params)
    @post.image = posts_params["image"]
    # @post = Post.create!(params)
    # @image = @post.image.build

    if @post.save
      redirect_to "/"
    else
      render action 'new'
    end

    # redirect_to posts_url
  end

  def new
    @post = Post.new
  end

  def posts_params
    params.require(:post).permit(:title, :content, :image, attachments: [])
  end

  def image
    @post = Post.find(params[:id])
    content = @post.image.read
    if stale?(etag: content, last_modified: @post.updated_at.utc, public: true)
      send_data content, type: @post.image.file.content_type, disposition: "inline"
      expires_in 0, public: true
    end
  end
end

But I haven`t any ideas how to process this multiple upload image files.. What I should to do in controller? How to get attachment array in view? Please help.

Aucun commentaire:

Enregistrer un commentaire