ruby-on-rails – form_for [@nested,@ resource],remote => true response with format.html rails 3.2.6

无法获得ajax form_for以尊重format.js respond_to仅响应format.html.任何帮助非常感谢.

视图

此视图由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
最佳答案
确保包含rails内置的非侵入式jquery插件,因为它负责制作远程:真正的代码工作.

另外,如果要渲染模板create.js.erb,则需要保留行format.js而不使用块:

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

除此之外,您使用其他响应格式吗? json和html?如果没有,尽量避免放置它们.

转载注明原文:ruby-on-rails – form_for [@nested,@ resource],remote => true response with format.html rails 3.2.6 - 代码日志