JavaScript 未加载最新提交的数据

标签 javascript jquery ruby-on-rails ruby ruby-on-rails-3

我最近问了这个问题:Javascript Form Submition

现在我试图弄清楚为什么在提交表单后,create.js.erb不加载最新数据(已提交的数据)。

如果我再次提交数据,表格将使用倒数第二个数据更新,但不是绝对最新的数据。

...为什么?

编辑:

最新代码 -

类别 Controller : 类 Admin::CategoriesController < Admin::AdminController ...

  def create
    @category = Admin::Category.new(params[:admin_category])
    # You can't retrieve the @categories before attempting to add new one. Keep at end of create method.
    @categories = Admin::Category.all
    respond_to do |format|
      if @category.save
        save_to_history("The category \"#{@category.name}\" has been created", current_user.id)
        format.json { render json: { html: render_to_string(partial: 'categories') } }
        # I've tried both the above and bellow
        #format.json { render json: { html: render_to_string('categories') } }
        format.html { redirect_to(admin_categories_url, :notice => 'Category was successfully created.') }
        format.xml  { render :xml => @category, :status => :created, :location => @category }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
      end
    end

  end

  ...
end

Javascript:

<script type='text/javascript'>
  $(function(){
    $('#new_admin_category').on('ajax:success', function(event, data, status, xhr){
      $("#dashboard_categories").html(data.html);
    });
  });
</script>

已解决

我使用@PinnyM的方法并添加了一个create.json.erb文件,其中包含以下代码:

<% self.formats = ["html"] %>
{
  "html":"<%= raw escape_javascript( render :partial => 'categories', :content_type => 'text/html') %>"
}

并将我的创建方法更改为:

def create
    @category = Admin::Category.new(params[:admin_category])

    respond_to do |format|
      if @category.save
        # You can't retrieve the @categories before attempting to add new one. Keep after save.
        @categories = Admin::Category.all
        save_to_history("The category \"#{@category.name}\" has been created", current_user.id)
        format.json 
        format.html { redirect_to(admin_categories_url, :notice => 'Category was successfully created.') }
        format.xml  { render :xml => @category, :status => :created, :location => @category }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
      end
    end
  end

如果这很困惑,请提供建议。

最佳答案

您需要处理远程 (AJAX) 提交的成功回调。 data 参数(第二个参数)保存响应:

$(function(){
  $('#new_category').on('ajax:success', function(event, data, status, xhr){
    eval(data);
  });
});

更好的方法(并避免危险的eval)可能是只返回部分内容,并让回调决定如何处理它:

# in create action
format.json { render json: { html: render_to_string('categories') } }

# in your js, run at page load
$(function(){
  $('#new_category').on('ajax:success', function(event, data, status, xhr){
    $("#dashboard_categories").html(data.html);
  });
});

更新

刚刚注意到@RomanSpiridonov 写的 - 他是绝对正确的。在尝试添加新类别之前,您无法检索@类别。将行 @categories = ... 移至 create 方法的末尾。

此外,我注意到您的类别模型是命名空间的 - 这意味着您的默认表单 id 属性更可能类似于“new_admin_category”。您应该检查它的实际渲染方式,并在注册成功回调时在 jQuery 选择器中使用该 id:

$(function(){
  $('#new_admin_category').on('ajax:success', function(...

关于JavaScript 未加载最新提交的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16524506/

相关文章:

php - 如何在 PHP 中调用函数来使用 HighCharts 制作图表

javascript - Jquery函数获取文本并相应地改变div样式

javascript - FileReader readAsBinaryString() 到图像 DOM 元素

javascript - jQuery - 删除多维数组中选定对象的方法

jquery - 数据表分页最初不起作用

javascript - 在 JS 中创建页面的内容拆分

当我尝试 rake db :create 时,Mysql 访问被拒绝错误

javascript - JSDoc 类型转换为 `window` ?

ruby-on-rails - Factory Girl + Mongoid 在装置中嵌入文档

ruby-on-rails - 将代码从github转移到我自己的git服务器