我有以下设置
查看
<%= form_for([:host, @host_gallery], :url => {:action => "create_photo"}, :html => {:multipart => true, :class => 'form-horizontal', 'role' => 'form'}) do |f| %>
<div class="form-group">
<%= f.label :file_name, "Photo:".html_safe,:class => 'col-sm-3 control-label' %>
<div class="col-sm-8">
<%= f.file_field :file_name, :multiple => true, accept: 'image/png,image/gif,image/jpeg' %>
<p class="help-block">
Choose a large Photo
</p>
<%= show_errors(@host_gallery, :file_name).html_safe %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-8">
<button type="submit" class="btn btn-ph">Upload Photo</button>
</div>
</div>
<% end %>
控制器
def create_photo
params[:host_gallery].each{ |image|
puts "======================================================"
puts image.inspect
puts "==========================END============================"
}
# render plain: params.inspect
@host_gallery = HostGallery.new(host_gallery_params)
@host_gallery.host_id = current_host.id
if @host_gallery.save
redirect_to host_profile_photos_url, notice: 'Photo was uploaded successfully created.'
else
flash[:error] = "Event could not be save. Please try again."
render :new_photo
end
end
private
# Only allow a trusted parameter "white list" through.
def host_gallery_params
params.require(:host_gallery).permit(:file_name)
end
现在,当我尝试选择多个图像并提交时,验证失败。 在控制台中显示此
Started POST "/host/profile/photos/new" for 127.0.0.1 at 2015-09-11 16:07:44 +0530
Processing by Host::ProfileController#create_photo as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6xo/+KoKHsdQxKYgMpSt337p/z1nIIPwy8aCOb//Qok=", "host_gallery"=>{"file_name"=>[#<ActionDispatch::Http::UploadedFile:0x007fe409c586c8 @tempfile=#<Tempfile:/var/folders/y8/g0hc4kb54jq0mlhsqlh6clc40000gn/T/RackMultipart20150911-2195-1edubzs>, @original_filename="AeeSyOb.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"host_gallery[file_name][]\"; filename=\"AeeSyOb.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fe409c586a0 @tempfile=#<Tempfile:/var/folders/y8/g0hc4kb54jq0mlhsqlh6clc40000gn/T/RackMultipart20150911-2195-14vmf57>, @original_filename="10996767_460989807398038_4893798671723861484_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"host_gallery[file_name][]\"; filename=\"10996767_460989807398038_4893798671723861484_n.jpg\"\r\nContent-Type: image/jpeg\r\n">]}}
Geokit is using the domain: localhost
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
======================================================
["file_name", [#<ActionDispatch::Http::UploadedFile:0x007fe409c586c8 @tempfile=#<Tempfile:/var/folders/y8/g0hc4kb54jq0mlhsqlh6clc40000gn/T/RackMultipart20150911-2195-1edubzs>, @original_filename="AeeSyOb.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"host_gallery[file_name][]\"; filename=\"AeeSyOb.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fe409c586a0 @tempfile=#<Tempfile:/var/folders/y8/g0hc4kb54jq0mlhsqlh6clc40000gn/T/RackMultipart20150911-2195-14vmf57>, @original_filename="10996767_460989807398038_4893798671723861484_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"host_gallery[file_name][]\"; filename=\"10996767_460989807398038_4893798671723861484_n.jpg\"\r\nContent-Type: image/jpeg\r\n">]]
==========================END============================
Unpermitted parameters: file_name
答案 0 :(得分:1)
def host_gallery_params
params.require(:host_gallery).permit(file_name: [:filename])
end
试试这个