Rails 4远程true和响应js
Rails 4 remote true and response js
我可以用表单中的remote:true
提交我的表单,并通过ajax成功发送。但我想用JS显示响应(成功或错误),但我做不到。
这是我的表格+JS:
= simple_form_for @widget, :url=> template_save_widget_manager_template_url(id:widget.id),:format => :js, remote: true do |f|
= f.input :title, label: "Titre ?"
= f.input :active, label: "Titre ?"
= f.input :where, label: "Ou souhaitez-vous afficher ce bloc ?", collection: @where
= f.button :submit, class:'btn btn-primary'
- content_for :script do
javascript:
$(document).ready(function(){
$("#new_clan_templates_widgets_build").on("ajax:success", function (e, data, status, xhr){
alert('test');
});
})
在我的控制器中,我只需要添加render false
。
javascript代码不起作用,为什么?
this is my code for creating a post with remote
<div class="row">
<div class="col-md-6">
<%= form_for(@blog, html: {role: 'form'}, remote: true) do |f| %>
<% if @blog.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@blog.errors.count, "error") %> prohibited this blog from being saved:</h2>
<ul>
<% @blog.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :title %><br>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :content %><br>
<%= f.text_area :content, class: "form-control" %>
</div>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.submit nil, class: "btn btn-primary" %>
<% end %>
</div>
</div>
and this is my create action in controller
def create
@blog = Blog.new(blog_params)
respond_to do |format|
if @blog.save
format.html { redirect_to @blog, notice: 'Blog was successfully created.' }
format.json { render :show, status: :created, location: @blog }
format.js{}
else
format.html { render :new }
format.json { render json: @blog.errors, status: :unprocessable_entity }
format.js { }
end
end
end
this is response file after success
<% if @blog.new_record? %>
alert('errors');
<% else %>
alert('post created successfully');
window.location = '<%= blog_path(@blog) %>'
<% end %>
好了,现在可以工作了。解决方案如下:
我已将添加到我的控制器中
class ExampleController < ApplicationController
layout proc{|c| c.request.xhr? ? false : "application" }
def create
respond_to do |format|
format.js { render :layout => !request.xhr? }
end
end
相关文章:
- 预期响应包含一个对象,但在angular js中得到一个数组错误
- 使Intro.js工具提示响应
- angular.js我如何设置数据什么是响应值
- Ember.js:将Em.$.getJSON转换为promise并将响应绑定到控制器上下文的正确方法
- D3.js:将一个有响应的饼图放在其父分区的中心
- 使用Backbone.js访问JSON响应的部分
- 引导:响应式设计-当窗口大小从980px调整到979px时执行JS
- 为什么新js Fetch标准禁止响应头名称为'设置Cookie 1/2'
- JS-通用解析JSON响应
- 分析angular.js中的JSONP$http.json()响应
- Rails 4远程true和响应js
- 为什么我的rails应用程序在生产环境中没有正确响应.js格式
- 如何使用响应式iframes使iframe响应.js
- 如何在没有 Web 服务器的情况下做出响应.js工作
- 响应.js将首选媒体查询而不是稍后定义的全局选择器
- IE8 支持使用响应.js进行媒体查询
- ie8响应js'未定义'为null或不是对象
- mobilesafari 5在下一次行动之前没有响应js事件
- 命令按钮不响应JS函数
- 如何在向第三方 API 节点发出多个请求后发送响应.js.