2014-05-20 112 views
3

4處理AJAX文件上傳在我HTML視圖代碼,我有兩個不同的形式,但共用同一個ID名稱困惑如何導軌和回形針

<div class="editcompanyprofile_tab" style="display:block;"> 
<div class="inputfield-container clearfix"> 
    <%= form_for(@company, remote: true) do |f| %> 
    <div class="inputfield_row clearfix"> 
    <div class="field" style ="width: 125px;"> 
     <%= f.label :name, "Company Name" %> 
    </div> 
    <div class="inputfield"> 
     <%= f.text_field :name, :style => "width: 259px;" %> 
    </div> 
    </div> 
    <div class="inputfield_row clearfix"> 
    <div class="field" style ="width: 125px;"> 
     <%= f.label :firstname, "First Name" %> 
    </div> 
    <div class="inputfield"> 
     <%= f.text_field :firstname, :style =>"width: 259px;" %> 
    </div> 
    </div> 
    <div class="inputfield_row clearfix"> 
    <div class="field" style ="width: 125px;"> 
     <%= f.label :lastname, "Last Name" %> 
    </div> 
    <div class="inputfield"> 
     <%= f.text_field :lastname, :style =>"width: 259px;" %> 
    </div> 
    </div> 
    <div class="inputfield_row clearfix"> 
    <div class="field" style ="width: 125px;"> 
     <%= f.label :title, "Title"%> 
    </div> 
    <div class="inputfield"> 

     <%= f.text_field :title, :style =>"width: 259px;"%> 
    </div> 
    </div> 
    <div class="inputfield_row clearfix"> 
    <div class="field" style ="width: 125px;"> 
     <%= f.label :email, "Email" %> 
    </div> 
    <div class="inputfield"> 
     <%= f.text_field :email, :style =>"width: 259px;" %> 
    </div> 
    </div> 
    <div class="inputfield_row clearfix"> 
    <div class="field" style ="width: 125px;"> 
     <%= f.label :phonenumber, "Work Number" %> 
    </div> 
    <div class="inputfield"> 
     <%= f.text_field :phonenumber, :style =>"width: 259px;" %> 
    </div> 
    </div> 
    <div class="inputfield_row clearfix" > 

    <div class="field" style="width: 40px;"> 
     <%= f.label :faxnumber, "Fax"%> 
    </div> 
    <div class="inputfield" > 
     <%= f.text_field :faxnumber, :style=>"width: 134px; margin-right: 5px;"%> 
    </div> 
    <div class="field" style="width: 60px;"> 
     <%= f.label :mobile, "Mobile"%> 
    </div> 
    <div class="inputfield"> 
     <%= f.text_field :mobile, :style=>"width: 129px;"%> 
    </div> 
    </div> 
    <div class="inputfield_row clearfix"> 
    <div class="field" style ="width: 125px;"> 
     <%= f.label :streetaddress, "Street Address"%> 
    </div> 
    <div class="inputfield"> 
     <%= f.text_field :streetaddress, :style=>"width: 259px;" %> 
    </div> 
    </div> 
    <div class="inputfield_row clearfix"> 
    <div class="field" style="width: 40px;"> 
     <%= f.label :city, "City" %> 
    </div> 
    <div class="inputfield" > 
     <%= f.text_field :city, :style =>"width: 134px; margin-right:5px;"%> 
    </div> 
    <div class="field" style="width: 60px;"> 
     <%= f.label :state, "State"%> 
    </div> 
    <div class="inputfield"> 
     <%= f.text_field :state, :style=>"width: 129px;" %> 
    </div> 
    </div> 
    <div class="inputfield_row clearfix"> 
    <div class="inputfield"> 
     <%= f.text_area :notes, :placeholder =>'Notes' %> 
    </div> 
    </div> 
    <div class="inputfield_buttons"> 
    <div class="inputaction_hc savechanges" style="margin-right:0; width:120px;"> 
     <%= f.submit "Save Changes", :name =>"SAVE", :value =>"Save Changes", :style=>"float: left;" %> 
    </div> 
    <div class="inputaction_hc cancel"> 
     <input type="button" name="CANCEL" value="Cancel" onclick="hideDiv('editcompanyprofile-menu');" style="float: left;"> 
    </div> 
    </div> 
    <% end %> 

</div> 
</div> 

<div class="logodesign_tab" style="display:none;"> 
    <%= image_tag @company.logo.url(:thumb) %> 
    <%= form_for(@company, :remote=>true, :html => { :multipart => true }) do |form| %> 
     <%= form.file_field :logo, :class=>"button" %> 
     <%= form.submit "Submit", :name =>"SAVE", :value =>"Submit" %> 
     <input type="button" name="CANCEL" value="Cancel" onclick="hideDiv('editcompanyprofile-menu');"> 
    <% end %> 
</div> 

在我控制器,這個代碼塊將執行更新操作

def update 
    respond_to do |format| 
    if @company.update(company_params) 
     format.html { redirect_to @company, notice: 'Company was successfully updated.' } 
     format.json { head :no_content } 
    else 
     format.html { render action: 'edit' } 
     format.json { render json: @company.errors, status: :unprocessable_entity } 
    end 
    end 
end 

的JS Ajax代碼

$("form[id^=edit_company]").on("ajax:success", function(e, data, status, xhr){ 
     alert('ajax succeeded!'); 
     window.location.reload(true); 
}); 

編輯companyprofile_tab div標記,當我編輯公司的任何字段並保存更改時,上面的js ajax代碼被執行並且觸發警報消息。但對於logodesign_tab div標籤,當我上傳標誌圖像並保存更改,相同的Ajax代碼上面並沒有得到執行!因此警報消息沒有彈出。

這實在令人費解,因爲這兩種兩種形式共享相同的ID即ID^= edit_company所以怎麼來的editcompanyprofile_tab的形式作品,但不是爲logondesign的形式????我在這裏錯過了什麼?!?!?

+0

如果切換徽標文件場用簡單的文本字段,它的工作原理? – crispychicken

回答

6

不幸的是,你不能用remote: true上傳文件。 AJAX上傳不適用於文件,至少不以默認方式。

可能的解決方案:

  1. Remotipart Gem
  2. HTML5 Formdata Object
  3. JQuery File-Upload

看一看這個問題:How can I upload files asynchronously with jQuery?

更新01/2015:

有一個新的寶石叫Refile,使異步文件上傳要容易得多。還有上GoRails.com一個很好的介紹視頻:File Uploads with Refile

+0

那麼可能解釋它。難道遇到這些紅寶石寶石來解決AJAX文件上傳的問題,如這些。在親愛的...另一個紅寶石寶石,我需要學習如何讓事情工作...感謝您的幫助很大! – awongCM