Ruby on rails 4 使用simple_forms simple_field_与Carrierwave的多部分文件上载程序
我正在创建一个博客,允许我在创建博客文章时上载多个图像文件。我正在使用Rails4和简单的表单gem。我无法获得允许选择多个文件的文件输入,尽管单个文件的上载工作正常 这是我的表格:Ruby on rails 4 使用simple_forms simple_field_与Carrierwave的多部分文件上载程序,ruby-on-rails-4,carrierwave,simple-form,Ruby On Rails 4,Carrierwave,Simple Form,我正在创建一个博客,允许我在创建博客文章时上载多个图像文件。我正在使用Rails4和简单的表单gem。我无法获得允许选择多个文件的文件输入,尽管单个文件的上载工作正常 这是我的表格: <%= simple_form_for(@post, :html => { :multipart => true }) do |f| %> <% if @post.errors.any? %> <div id="error_explanation">
<%= 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=>true})do | f |%>
禁止保存此帖子:
true,名称:“post_附件[媒体][]”%>
$(函数(){
$(“#制表符”).tabs();
});
我怀疑“multipart”属性输入不正确。除了以下网站上的声明外,我似乎找不到任何关于此的文档:
包装器,用于在默认rails窗体内使用简单窗体。其工作方式与rails helper的字段_相同,但将生成器更改为使用SimpleForm::FormBuilder
我不明白在这里具体做什么。我希望能就此问题提供一些指导
更新:这是我的控制器代码
posts\u 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
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
class PostsControllera,:post_id=>@post.id)
结束
结束
format.html{将_重定向到@post,注意:'post已成功创建。}
其他的
format.html{呈现操作:'new'}
结束
结束
#@post.save
#用(@post)回复_
结束
def更新
@post.update(post_参数)
用(@post)回复_
结束
def销毁
@事后销毁
用(@post)回复_
结束
私有的
def set_柱
@post=post.find(参数[:id])
结束
def post_参数
参数require(:post).permit(:title,:subtitle,:author,:content,:publish,:quote\u author,:quote\u text,:quote\u source,:video\u link,post\u attachments\u attributes:[:id,:post\u id,:media])
结束
def解析单元布局
案例行动名称
“编辑”、“新建”、“创建”、“索引”时
“全宽”
当“显示”、“列表”时
“博客”
其他的
“基地”
结束
结束
结束
发布\u附件\u 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
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
class PostAttachmentsController
以下是我的模型:
post.rb
class Post < ActiveRecord::Base
has_many :post_attachments
accepts_nested_attributes_for :post_attachments
end
class Post
post_attachment.rb
class PostAttachment < ActiveRecord::Base
mount_uploader :media, MediaUploader
belongs_to :post
end
class PostAttachment
尝试更改此行
<%= p.input :media, as: :file, :multiple => true, name: "post_attachments[media][]" %>
将其更改为:
<%= i.input :media, as: :file, input_html: { multiple: true, name: "post_attachments[media][]" } %>
这应该可以解决问题。这不起作用。但是,我发现这至少可以让我选择多个文件:
{:multiple=>true},名称:“post_attachments[media][]”%>
,但它实际上并没有保存多个文件。它只保存一个文件。@U north更新了我的答案。请检查。这些更改无效。我无法再选择多个文件。请发布控制器代码。谢谢。我已经发布了控制器和型号代码。