我正在创建一个博客,当我创建博客文章时,该博客允许我上传多个图像文件。我正在将Rails 4与简单形式的gem一起使用。我无法获取文件输入以允许选择多个文件,尽管上载单个文件也可以。
这是我的表格:
<%= 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。
我不知道在这里专门做什么。我希望能对此问题提供一些指导。
更新:这是我的控制器代码
posts_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
posts_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
这是我的模特:
post.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
我正在创建一个博客,当我创建博客文章时,该博客允许我上传多个图像文件。我正在将Rails 4与简单形式的gem一起使用。我无法获取文件输入以允许选择...