我正在尝试做一个remote: true
表格,我已经完成了所有设置,当我单击提交按钮时,每次都会触发错误操作。这就是我的 CoffeeScript 的设置方式:
$(document).ready ->
$("#new_report").on("ajax:success", (e, data, status, xhr) ->
$("#new_report").append "<p>SUCCESS</p>"
).bind "ajax:error", (e, xhr, status, error) ->
$("#new_report").append "<p>ERROR</p>"
还有我的 Reports_Controller
def create
@report = Report.new(params[:report])
respond_to do |format|
if @report.save
format.json { render json: "Created", :status => :created }
else
format.json { render json: @report.errors, status: :unprocessable_entity }
end
end
我的表格:
<%= form_for(@report, remote: true ) do |t| %>
<p id="reportalert"></p>
<%= t.text_field :plant_site, placeholder: "Plant Site" %>
<%= t.text_field :route_number, placeholder: "Route Number" %>
<%= t.text_field :driver_name, placeholder: "Driver name if available" %>
<%= t.date_select :date_recorded, html: { class: "input-block-level" } %>
<%= t.text_field :action, placeholder: "Action taken" %>
<%= t.text_area :report_body, placeholder: "What you witnessed",
style: "height: 300px;",
class: "input-block-level" %>
<%= t.submit "File Report", class: "btn btn-primary btn-large" %>
<% end %>
但是我查看rails日志,只有200响应,没有错误,而且信息已经提交到数据库,为什么会ajax:error
触发呢?我也尝试过用ajax:error
andajax:success
替换ajaxSuccess
andajaxError
但它什么也没做。任何信息都会非常感谢。
我正在尝试关注:http ://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html