Javascript未加载最新提交的数据

Javascript未加载最新提交的数据,javascript,jquery,ruby-on-rails,ruby,ruby-on-rails-3,Javascript,Jquery,Ruby On Rails,Ruby,Ruby On Rails 3,我最近问了一个问题: 现在我想弄清楚为什么在表单提交之后,没有加载最新的数据(提交的数据) 如果我再次提交数据,表将使用最后第二个数据更新,而不是绝对最新的数据 …为什么 编辑: 最新代码- 类别控制员: 类管理员::分类控制器

我最近问了一个问题:

现在我想弄清楚为什么在表单提交之后,没有加载最新的数据(提交的数据)

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

…为什么

编辑:

最新代码-

类别控制员: 类管理员::分类控制器<管理员::管理员控制器

  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>

如果这很混乱,请提供建议。

您需要处理远程(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。将行
@categories=…
移动到
create
方法的末尾

另外,我注意到您的类别模型是有名称空间的-这意味着您的默认表单
id
属性更像是“new\u admin\u Category”。注册成功回调时,应检查它的实际呈现方式,并在jQuery选择器中使用该id:

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

保存新类别之前定义的方法“create”中的实例变量@categories。这就是为什么您无法接收模板中的最新类别。 我想你可以这样写:


$(“#仪表板_类别”)。追加(“”)

您可以将这段javascript添加到
资产/javascripts
文件夹中的文件中

$(your_form_selector).on('ajax:success', function(){
  // Read link to see what event names you can use instead of ajax:success and
  // which arguments this function provides you.
})
要设置
表单选择器
,您可以在表单中添加id并使用
#myFormId


阅读更多内容

你是认真的吗
eval
在回答一个问题时,标记了不引人注目的JavaScript,并在没有jQ标记的情况下假设jQ?@EliasVanOotegem:就是这样。如果没有
eval
,浏览器就无法解析被传回的javascript,这就是为什么我建议不要传回任何javascript。关于jQ——在链接的问题中,公认的答案是使用
jquery\u ujs
——这需要使用jquery。是的,试图在链接的代码中发现JS,我也注意到了jQ。虽然我很难将OP的意图归类为符合不引人注目的JS标签。。。也许最好重新回答这个问题:)很抱歉搞混了。那么我如何实现这一点呢?我刚试过,但也许我错过了什么?对不起,你得握住我的手,我是新手@TaylorWilliams:将您的操作更改为使用
format.json
而不是
format.js
,在categories.js文件或页面上的任何位置注册“ajax:success”回调(使用
$(function(){…})
加载页面后运行),您可以删除不需要的
create.json.erb
。如果这对您不起作用,也许可以发布您修改后的代码-并请将其内联发布(pastie很好,但不可靠)。您可以粘贴您试图处理AJAX事件的代码吗?
$(function(){
  $('#new_admin_category').on('ajax:success', function(...
$(your_form_selector).on('ajax:success', function(){
  // Read link to see what event names you can use instead of ajax:success and
  // which arguments this function provides you.
})