将simple_forms simple_field_for与Carrierwave的多部分文件上传器一起使用

时间:2015-07-07 20:38:35

标签: ruby-on-rails-4 carrierwave simple-form

我正在创建一个博客,允许我在创建博客帖子时上传多个图像文件。我使用简单形式的宝石Rails 4。我无法获取文件输入以允许选择多个文件,尽管单个文件的上传工作正常。

这是我的表格:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, only: [:index, :edit, :update, :destroy]
  layout :resolve_layout

  respond_to :html

  def index
    @posts = Post.all
    respond_with(@posts)
  end

  def list
    @posts = Post.all
    respond_with(@posts)
  end

  def show
    @post_attachments = @post.post_attachments.all
    respond_with(@post)
  end

  def new
    @post = Post.new
    @post_attachment = @post.post_attachments.build
    respond_with(@post)
  end

  def edit
  end

  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        if (@post_attachments != nil)
          params[:post_attachments]['media'].each do |a|
            @post_attachment = @post.post_attachments.create!(:media => a, :post_id => @post.id)
          end
        end

        format.html { redirect_to @post, notice: 'Post was successfully created.'}
      else
        format.html { render action: 'new' }
      end
    end
    # @post.save
    # respond_with(@post)
  end

  def update
    @post.update(post_params)
    respond_with(@post)
  end

  def destroy
    @post.destroy
    respond_with(@post)
  end

  private
    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.require(:post).permit(:title, :subtitle, :author, :content, :publish, :quote_author, :quote_text, :quote_source, :video_link, post_attachments_attributes: [:id, :post_id, :media])
    end

    def resolve_layout
      case action_name
      when "edit", "new", "create", "index"
        "full_width"
      when "show", "list"
        "blog_rt"
      else
        "base"
    end
  end
end

我怀疑&#34; multipart&#34;属性未正确输入。除了simple_form wiki上的声明外,我似乎无法找到关于此的任何文档:

&#34; Wrapper在默认的rails表单中使用Simple Form。它的工作方式与fields_for Rails helper相同,但更改构建器以使用SimpleForm :: FormBuilder。&#34;

我不明白这里要做什么。我会很感激这个问题的一些指导。

更新:这是我的控制器代码

posts_controller.rb

  class PostAttachmentsController < ApplicationController
  before_action :set_post_attachment, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @post_attachments = PostAttachment.all
    respond_with(@post_attachments)
  end

  def show
    respond_with(@post_attachment)
  end

  def new
    @post_attachment = PostAttachment.new
    respond_with(@post_attachment)
  end

  def edit
  end

  def create
    @post_attachment = PostAttachment.new(post_attachment_params)
    @post_attachment.save
    respond_with(@post_attachment)
  end

  def update
    @post_attachment.update(post_attachment_params)
    respond_with(@post_attachment)
  end

  def destroy
    @post_attachment.destroy
    respond_with(@post_attachment)
  end

  private
    def set_post_attachment
      @post_attachment = PostAttachment.find(params[:id])
    end

    def post_attachment_params
      params.require(:post_attachment).permit(:post_id, :media)
    end
end

posts_attachments_controller.rb

class Post < ActiveRecord::Base
    has_many :post_attachments
    accepts_nested_attributes_for :post_attachments
end

以下是我的模特:

post.rb

class PostAttachment < ActiveRecord::Base
    mount_uploader :media, MediaUploader
    belongs_to :post 
end

post_attachment.rb

play

2 个答案:

答案 0 :(得分:1)

尝试更改此行

<%= p.input :media, as: :file, :multiple => true, name: "post_attachments[media][]" %>

<%= p.input :media, as: :file, :multiple => true, name: "post[post_attachments_attributes][][media]" %>

<强> 更新

您应该在:media的{​​{1}}方法中将:media => []更改为post_params以保存多个值。

posts_controller

Source

答案 1 :(得分:0)

将其更改为此:

<%= i.input :media, as: :file, input_html: { multiple: true, name: "post_attachments[media][]" } %>

这应该可以解决问题。