Javascript not loading latest submitted data

穿精又带淫゛_ 提交于 2019-12-25 04:14:05

问题


I recently asked this question: Javascript Form Submition?

Now I'm trying to work out why, after form submission, the create.js.erb doesn't load the latest data (the data which was submitted).

If I submit the data again, the table will update with the second last data but not the absolute latest.

...why?

EDIT:

Latest code -

Categories controller: class 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>

SOLVED

I used @PinnyM's method and added a create.json.erb file with the following code:

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

and changed my create method to:

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

Please do offer suggestions if this is messy.


回答1:


You need to handle the success callback for your remote (AJAX) submission. The data parameter (2nd argument) holds the response:

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

A better way to do this (and avoid the dangerous eval) might be to just return the partial, and have the callback decide what to do with it:

# 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);
  });
});

UPDATE

Just noticed what @RomanSpiridonov wrote - he's absolutely correct. You can't retrieve the @categories before attempting to add your new one. Move the line @categories = ... to the end of the create method.

Also, I noticed that your Category model is namespaced - that means your default form id attribute is more likely something like 'new_admin_category'. You should check how it is actually being rendered and use that id in your jQuery selector when registering the success callback:

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



回答2:


Instance variable @categories in method "create" defined before saving new category. That's why you can't receive the lastest category in your template. I think you could write something like that:

$("#dashboard_categories").append("<%= escape_javascript(render("category")) %>");




回答3:


You could add this piece of javascript in a file inside assets/javascripts folder

$(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.
})

To set your_form_selector you could add an id to your form and use #myFormId

Read more here



来源:https://stackoverflow.com/questions/16524506/javascript-not-loading-latest-submitted-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!