Tag: ajax

Rails设计多态 – 使用Ajax渲染部分渲染

我已经从这个答案实现了多个模型的设计注册。 这个tuts从路径获取params user_type 。 我想用select user_type更改它。 因此,当我在select_tag上选择一个值时,将获得一个param user_type。 我有一些代码看起来像: 的routes.rb namespace :cp do devise_scope :user do match ‘/add_user’ => ‘registrations#new’ match ‘/select/admin’ => ‘registrations#selectuser’, :user => { :usertype => ‘admin’ } match ‘/select/player’ => ‘registrations#selectuser’, :user => { :usertype => ‘player’ } end end registrations_controller.rb def selectuser respond_to do |format| format.js end end new.html.erb Add […]

在Rails中通过JS设计注册#update

我正在尝试使用AJAX通过Devise更新用户模型,并让Devise使用正确的javascript文件进行响应。 我想将表单远程提交到注册#update action,但这不适用于Devise的默认响应,它使用RegistrationsController中的以下内容: respond_with resource, :location => after_update_path_for(resource) 上面尝试重定向到默认路由,而不是呈现update.js.erb文件。 我可以覆盖该操作并使其与以下更改一起使用: respond_to do |format| format.html format.js end 但是,这似乎非常暴力,因为我压倒整个行动。 是否有一种简单的方法让Devise知道用javascript响应而不是默认重定向?

带Rails的Dropzonejs – 从服务器删除文件

我正在使用dropzonejs-rails gem并使用dropzone进行文件管理。 我上传工作很棒,但删除文件让我感到痛苦。 我无法让侦听器附加,以便ajax将命中我的服务器。 我的强项是Ruby on Rails,我只有有限的javascript经验,所以这可能是真正的问题……但也许有人可以帮助我解决我[可耻的] 2天的斗争。 Dropzone在上传文件后正在dropzone中正确显示previewTemplate, addRemoveLinks: true在缩略图预览上显示删除链接。 我的目标是在用户单击文件的删除链接时,应该向服务器发送请求以删除该文件。 我尝试了许多不同的方法来自博客,dropzone FAQ,github问题等。我最接近成功的是: https : //github.com/enyo/dropzone/issues/456 。 我尝试在成功上传后向.dz-remove按钮添加一个事件监听器。 此侦听器旨在使用ajax请求命中服务器以删除该文件。 目前,单击预览缩略图上的删除链接仅从dropzone中删除预览缩略图,但不会使用ajax命中服务器。 这是javascript代码: // Listening for Dropzone upload success // imageDropzone is the camelized version of the dropzone form’s id. Dropzone.options.imageDropzone = { init: function() { this.on(“success”, function(file, response, event) { // Loads image ID to […]

使用Rails实时预览格式化的HTML表单输入

我想要一个function,用HTML标签预览一些文本,然后将文本存储在数据库中。 出于XSS安全原因,我知道在数据库中允许HTML不是一个好主意。 有什么方法可以达到这个目的? 我想要一个类似于stackoverflow中的function,我们可以格式化我们的源代码。 谢谢。

模板因Ajax请求而丢失错误 – Rails 3.1

我有一个带有Ruby 1.8.7的rails 3.1应用程序。 我正在尝试通过ajax创建联系人,但不断收到模板错误。 ActionView::MissingTemplate (Missing template contacts/create, application/create with {:handlers=>[:coffee, :builder, :erb], :locale=>[:en, :en], :formats=>[:html]}. Searched in: * “/Users/Me/Sites/t3/app/views” ): 表格: true do |f| %> @customer.id %> “width:120px;”, :class => “contact_input” %> “width:120px;”, :class => “contact_input” %> “width:120px;”, :class => “contact_input” %> “width:120px;”, :class => “contact_input” %> “width:60px;”, :class => “contact_input” %> “width:200px;”, :class […]

Javascript不是在脚本中呈现而是作为html呈现

这个问题是我昨天问过的一个问题,它涉及将模型的创建/编辑function移动到其索引页面上。 我遇到的最后一个问题是,当我去删除模型时,我有一些应该运行的javascript重新加载模型列表以反映数据库中的更改。 这个js被渲染为html。 以下是我认为相关的代码: 在我的控制器中: def destroy @post = Post.find(params[:id]) @post.destroy flash[:notice] = “Successfully destroyed post.” @posts = Post.all respond_to do |format| format.js {render :content_type => ‘text/javascript’ } end end 我的post部分列表(其中包含我所指的销毁链接): Title Content “edit” %> ‘Are you sure?’, :method => :delete, :class => ‘destroy’ %> destroy.js.erb: $(“#post_errors”).hide(300); $(“#flash_notice”).html(“”); $(“#flash_notice”).show(300); $(“#posts_list”).html(” “posts”) ) %>”); 在页面加载时加载的javscript: // […]

如何使用Rails从数字字段进行jQuery ajax调用

我正在做一个fixeddeposit项目,我有三个模型: 1) fixeddeposit 2) interestrate 3) interestsetup 当我打开fixeddeposit ,我必须从number_field输入天数(365/730/1095/1460和1825)。 然后在另一个文本框中,我想根据我键入的数字(365/730/1095/1460和1825)更新rate_of_interest 。 Rate_of_interest应取自利率表字段中的费率。 请参阅下面我感兴趣的表格的屏幕截图。 兴趣表 例: 如果我以固定存款forms输入数字365(即number_field)。 它使用AJAX函数从兴趣表中自动选择9.5%的费率。 并且,如果客户年龄超过58,并且我以固定存款forms输入数字365。 它使用AJAX函数从兴趣表中自动选择(率9.5%+高级增量0.5%)= 10.0%。 FD的屏幕截图 我用谷歌搜索了一个解决方案,但结果只显示了collection_select。 我想在number_field中做同样的事情。 虽然,我是Ruby on Rails和AJAX的新手,但我正在努力解决它。

Rails,Ajax,Post Function,Jquery

我正在研究Rails并且遇到ajax post数据调用的问题。 它正在工作,数据输入db但SUCCESSfunction不起作用,因为成功函数中的警报function未触发。 此外,我想在不刷新整个页面的情况下为我的post添加评论。 AJAX代码如下所示: $(document).ready(function () { $(‘form’).submit(function () { $.ajax({ url: ‘/comments/create’, type: “POST”, dataType: ‘json’, processData: false, success: function (msg) { alert(msg); }, error: function (xhr, status) { alert(xhr.error); } }); }); }); 和控制器代码是: def create puts ‘params’, params.inspect @post = Post.find(params[:post_id]) @comment = @post.comments.new(params[:comment]) respond_to do |format| if @comment.save format.html { […]

重定向发出PATCH请求而不是GET

我正在从我的application.js文件中发出一个ajax补丁请求: $.ajax({ type: “PATCH”, url: “/units/” + id, data: { ‘var_1’: var_1, ‘var_2’: var_2,}, success:function(){ alert(“Details saved successfully!!!”); }, dataType: “text” }); // stop normal form submission behaviour $(“#add_form”).submit(function(e){ return false; }); 我的控制器成功接收到此请求,并更新了我的对象的属性。 然后我尝试重定向到具有特定id的索引页面。 units_controller.rb if @unit.update_attributes( ** data ** ) flash.notice = “Successfully updated ” redirect_to :controller => “/units”, :action => “index”, :id => […]

从javascript onclick发出ajax请求

我习惯使用帮助方法link_to_remote和朋友来创建链接或按钮,这些链接或按钮将在引擎盖下发送Ajax请求并更新网页的一些数据。 我有一些背景图像是按钮。 我希望他们将Ajax请求发送到我的rails服务器。 我想一个javascript onClick +发送Ajax请求应该做的魔术,但我不知道Prototype如何在Rails帮助方法下工作。 我希望这不是一个太蹩脚的问题