重命名上传的文件Ruby On Rails
Rename file uploaded Ruby On Rails
我使用jquery multiupload rails来创建一个multiupload。
所以这项工作,但当我想修改我的文件的标题时,标题是相同的,用于2个文件,而不是文件1的标题1和文件2的标题2等…
我使用javascript来做这件事。
我的表单在javascript 中
<%= form_for Sound.new , :html => { :multipart => true, :id => "fileupload" } do |f| %>
<!-- Redirect browsers with JavaScript disabled to the origin page -->
<noscript><input type="hidden" name="redirect" value="http://blueimp.github.io/jQuery-File-Upload/"></noscript>
<!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
<div class="row fileupload-buttonbar">
<div class="span7">
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
<i class="icon-plus icon-white"></i>
<span>Add files...</span>
<%= f.file_field :file, multiple: true, id: 'upload-field' %>
</span>
<button type="submit" class="btn btn-primary start">
<i class="icon-upload icon-white"></i>
<span>Start upload</span>
</button>
<button type="reset" class="btn btn-warning cancel">
<i class="icon-ban-circle icon-white"></i>
<span>Cancel upload</span>
</button>
<button type="button" class="btn btn-danger delete">
<i class="icon-trash icon-white"></i>
<span>Delete</span>
</button>
<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload fade">
<td class="preview"><span class="fade"></span></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="title"><input name="title[]" value='{%=file.name%}' required></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
我知道我的错误在这一行:如果我使用title[],那么对于2个文件,标题是"title 1,title 2"。如果我只使用两个文件的标题,文件1和文件2只有"标题2"。
<td class="title"><input name="title[]" value='{%=file.name%}' required></td>
我的控制器#创建
def create
@sound = Sound.new(params[:sound])
@sound.title = params[:title].to_s.gsub(/'[|']|"/,'')
respond_to do |format|
if @sound.save
redirect_to sounds_path
else
format.html { render action: "new" }
format.json { render json: @sound.errors, status: :unprocessable_entity }
end
end
end
控制台
Started POST "/sounds" for 127.0.0.1 at 2013-12-17 17:59:09 +0100
Processing by SoundsController#create as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ke1wOlf1z+eoQ6cS0lpQraqsiiU0BXoT2VtFweGed18=", "title"=>"309060_4371516803467_1048226528_n.jpg", "sound"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007fa626cbff88 @original_filename="14591_4925922503263_818636474_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name='"sound[file]'"; filename='"14591_4925922503263_818636474_n.jpg'"'r'nContent-Type: image/jpeg'r'n", @tempfile=#<File:/var/folders/sj/sr703c8n6llc198jfg6746d40000gn/T/RackMultipart20131217-2706-itxsfb>>}}
(0.1ms) begin transaction
SQL (0.6ms) INSERT INTO "sounds" ("created_at", "file", "name", "title", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Tue, 17 Dec 2013 16:59:09 UTC +00:00], ["file", "14591_4925922503263_818636474_n.jpg"], ["name", nil], ["title", "309060_4371516803467_1048226528_n.jpg"], ["updated_at", Tue, 17 Dec 2013 16:59:09 UTC +00:00]]
(1.1ms) commit transaction
Redirected to http://localhost:3000/sounds
Completed 406 Not Acceptable in 14ms (ActiveRecord: 1.8ms)
Started POST "/sounds" for 127.0.0.1 at 2013-12-17 17:59:09 +0100
Processing by SoundsController#create as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ke1wOlf1z+eoQ6cS0lpQraqsiiU0BXoT2VtFweGed18=", "title"=>"309060_4371516803467_1048226528_n.jpg", "sound"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007fa625a8d9a0 @original_filename="25490_392719757235_767862235_3946737_600749_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name='"sound[file]'"; filename='"25490_392719757235_767862235_3946737_600749_n.jpg'"'r'nContent-Type: image/jpeg'r'n", @tempfile=#<File:/var/folders/sj/sr703c8n6llc198jfg6746d40000gn/T/RackMultipart20131217-2706-d32r2a>>}}
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "sounds" ("created_at", "file", "name", "title", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Tue, 17 Dec 2013 16:59:09 UTC +00:00], ["file", "25490_392719757235_767862235_3946737_600749_n.jpg"], ["name", nil], ["title", "309060_4371516803467_1048226528_n.jpg"], ["updated_at", Tue, 17 Dec 2013 16:59:09 UTC +00:00]]
(2.0ms) commit transaction
Redirected to http://localhost:3000/sounds
Completed 406 Not Acceptable in 8ms (ActiveRecord: 2.4ms)
谢谢你的帮助。
<td class="title"><input name="sound[title]" value='{%=file.name%}' required></td>
or
<td class="title"><span><input type="text" name="sound[title]"value="{%= file.name.split('/').pop().split('.').shift()%}" required/></span></td>
将其添加到提交回调中
$('#fileupload').bind('fileuploadsubmit', function (e, data) {
var inputs = data.context.find(':input');
if (inputs.filter('[required][value=""]').first().focus().length) {
return false;
}
data.formData = inputs.serializeArray();
});
.first()将获取第一个输入值。
相关文章:
- Ruby on Rails javascript getting truncated
- 在ruby on rails中使用jquery getJSON获取数据
- Ruby On Rails应用程序与angularJS关于AJAX
- ruby on rails:用于不同页面的选项卡
- Ruby On Rails Ajax 提交提交两次
- Ruby on Rails - 引导旋转木马按钮不起作用
- 按索引迭代数组时,单击提交按钮Ruby on Rails
- 如何将.js从视图移动到单独的资产 - Ruby On Rails
- 如何在Ruby on Rails中集成Arbor.js
- geolocation with javascript and ruby on rails
- Ruby on Rails 4:在 Rails Web 应用程序中添加 Javascript 文件时遇到问题
- Window.onload 未被调用 [Ruby on Rails] [Vanilla Javascript]
- 如何在Ruby on Rails会话过期时重新加载或刷新页面
- 重命名上传的文件Ruby On Rails
- Ruby on Rails - grouped_collection_select error
- 带有jquery.min.map的Ruby on Rails:未能加载资源状态406
- Ruby on rails-更新ajax的PUT方法
- Heroku Ruby on Rails应用程序don't更新公共/资产目录中的未消化资产
- Ruby on rails javascript没有;无法正确加载(HTML5模板)
- 谷歌图表API与Ruby on Rails