【问题标题】:Rename file uploaded Ruby On Rails重命名上传的文件 Ruby On Rails
【发布时间】:2013-12-17 15:32:18
【问题描述】:

我使用 jquery-multiupload-rails 来创建多重上传。

这样就可以了,但是当我想修改文件的标题时,两个文件的标题是相同的,而不是文件 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 file ,标题是“title1,title2”。如果我只对 2 个文件使用标题,则文件 1 和文件 2 仅具有“title2”。

<td class="title"><input name="title[]" value='{%=file.name%}' required></td>

还有我的控制器#create

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)

感谢您的帮助。

【问题讨论】:

  • 你用的是什么版本的rails?
  • 能否提供控制台输出
  • 我肯定会更新我的帖子
  • 如果我使用 title[] ,我的文件名包含所有标题,但我只想要 1 个标题为 1 的文件,第二个文件为第二个标题等...

标签: javascript jquery ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

<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() 将采用第一个输入值。

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 2010-11-25
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 2011-06-24
    • 2012-07-05
    相关资源
    最近更新 更多