ruby-on-rails-4 - 将 simple_forms simple_field_for 与 Carrierwave 的多部分文件 uploader 一起使用

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

我正在创建一个博客,允许我在创建博客文章时上传多个图像文件。我正在使用带有简单形式 gem 的 Rails 4。我无法获得文件输入以允许选择多个文件,但单个文件的上传工作正常。

这是我的表格:

<%= simple_form_for(@post, :html => { :multipart => true }) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2, class: "white"><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |message| %>
        <li, class: "white"><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :title %>
  <%= f.input :subtitle %>
  <%= f.input :author %>
  <%= f.input :content, as: :text %>
  <%= f.input :publish, as: :select %>
  <%= f.association :tags, as: :check_boxes %>

  <div id="tabs">
    <ul>
      <li><a href="#tabs-1">Upload Images</a></li>
      <li><a href="#tabs-2">Link to Video</a></li>
      <li><a href="#tabs-3">Quote</a></li>
    </ul>
    <div id="tabs-1">
        <%= f.simple_fields_for :post_attachments do |p| %>
         <div class="field">
           <%= p.input :media, as: :file, :multiple => true, name: "post_attachments[media][]" %>
         </div>
       <% end %>
       <div class="actions"></br>
          <%= f.button :submit %>
       </div>
    </div>
    <div id="tabs-2">
      <div class="field">
        <%= f.label :video_link %><br>
        <%= f.text_field :video_link %>
      </div>
      <div class="actions"></br>
          <%= f.button :submit %>
      </div>
    </div>
    <div id="tabs-3">
      <div class="field">
        <%= f.label :quote_text %><br>
        <%= f.text_field :quote_text %>
      </div>
      <div class="field">
        <%= f.label :quote_author %><br>
        <%= f.text_field :quote_author %>
      </div>
      <div class="field">
        <%= f.label :quote_source %><br>
        <%= f.text_field :quote_source %>
      </div>
      <div class="actions"></br>
          <%= f.button :submit %>
      </div>
    </div>
  </div>

<script>
  $(function() {
    $("#tabs").tabs();
  });
</script>
<% end %>

我怀疑未正确输入“multipart”属性。除了 simple_form wiki 上的声明之外,我似乎找不到任何关于此的文档。 :

“在默认 rails 表单中使用简单表单的包装器。它的工作方式与 fields_for Rails 助手相同,但将构建器更改为使用 SimpleForm::FormBuilder。”

我不明白在这里具体做什么。我将不胜感激有关此问题的一些指导。

更新:这是我的 Controller 代码

post_controller.rb
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

post_attachments_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

这是我的模型:

后.rb
class Post < ActiveRecord::Base
    has_many :post_attachments
    accepts_nested_attributes_for :post_attachments
end

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

最佳答案

尝试更改此行

<%= 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:media => []在您的 post_params您的方法 posts_controller保存多个值。
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

Source

关于ruby-on-rails-4 - 将 simple_forms simple_field_for 与 Carrierwave 的多部分文件 uploader 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31278820/

相关文章:

ruby-on-rails - Rails - 迁移错误 - PG::InvalidSchemaName: 错误:

ruby-on-rails - NoMethodError : undefined method `each' for #<Education id: nil, profile_id : nil, school_name:无>

ruby-on-rails - 在config/database.yml文件中存储生产环境的DB密码是否可以

ruby-on-rails - Rails 3 和载波

ruby-on-rails - Rails Carrierwave S3 获取带有 Content-Disposition header 的 url

javascript - 如何根据表单输入添加动态复选框?

devise - Ruby on Rails - 设计错误 : undefined local variable or method

ruby-on-rails - 载波NoMethodError : undefined method `image_url' for Item

javascript - Ruby on Rails - 使用 AJAX 更新输入选择选项

html - 简单形式 : disable form without adding disabled: true or readonly: true to every input