7

無法獲得ajax form_for來尊重format.js respond_to只響應format.html。任何幫助非常感謝。form_for [@nested,@resource],remote => true使用format.html rails進行響應3.2.6

視圖

這一觀點在部分由AJAX調用,然後在用戶提交的形式。最初的ajax調用可能會混淆這種形式的'remote:true'嗎?

<%= form_for([@nested, @nested.resources.new], remote: true) do |i|%> 
    <%= i.hidden_field :inviter_id, value: current_user.id %> 
    <%= i.hidden_field :fb_pic_url, value: f['pic_square'] %> 
    <%= i.hidden_field :name, value: f['name'] %> 
    <%= i.hidden_field :uid, value: f['uid'] %> 
    <%= i.submit "Invite", class:"btn btn-success invite_button" %> 
<% end %> 

的routes.rb

resources :nested do 
    resources :resources 
end 

控制器

def create 
    code code code 

    respond_to do |format| 
    format.html { redirect_to @nested, notice: "Successfully Posted Nested" } 
    format.json { render json: @nested, status: :created, location: @nested } 
    format.js { render :nothing => true } 
    end 
end 

create.js.erb 存在但爲空

application.html

<%= javascript_include_tag "application" %> 
<%= csrf_meta_tags %> 

的application.js

//= require jquery 
//= require jquery_ujs 
+0

在請求中發送了哪些接受的頭文件? – joshuacronemeyer

+0

「HTTP_ACCEPT」=>「text/html,application/xhtml + xml,application/xml; q = 0.9,*/*; q = 0.8」 – ajbraus

+0

我不明白這個問題。你是說在提交form_for後得到html迴應? – jizak

回答

2

請確保您有內置導軌非侵入式的jQuery插件包括在內,因爲它負責制定remote: true碼工作。

另外,如果你想呈現模板create.js.erb你需要離開該行format.js不帶有塊:

respond_to do |format| 
    ... 
    format.js 
end 

除此之外,您使用的是其他的響應格式? jsonhtml?如果沒有,儘量避免把它們。

+0

我和這個人有同樣的確切問題(同樣的確切!),我已經在你的答案中滿足你的所有建議,並且仍然在解決問題。你可以看看我已經寫在這裏的問題:[我的問題](http://stackoverflow.com/questions/22545354/remote-true-option-for-form-for-working-for-new -action的但不創建行動/ 22673922#22673922)? –

相關問題