Search code examples
ruby-on-railsbest-in-place

How to access the updated object in a javascript callback (instead of the old object)


Building on the helpful and working solution presented here, I'm trying to fix my update callback as well.

Problem is, the specific unit that I'm trying to extract data from is always the old cached version, even though this callback is triggered by a successful update action.

// callback triggered by the update action

$('.best_in_place').bind("ajax:success", function () {
   ...
    console.log(unit.duration);
    // which is exactly the same as
    console.log(<%= Unit.find(unit.id).unit_users.pluck(:duration).sum %>);
    // and both print the OLD duration val instead of the updated val which is in the database
});

and the unit_users_controller code...

  def update
    @unit = @unituser.unit

    respond_to do |format|
      if @unituser.update(unit_user_params)
        @unit.reload
        logger.info('-----------------------------------------------------------------')
        logger.info('@unit.duration in the controller is ' + @unit.duration.to_s) # which is the correct value
        logger.info('-----------------------------------------------------------------')
        gon.unit_duration = @unit.duration # an experiment which didn't work for me
        format.json {respond_with_bip(@unituser) }
      else
        # format.html { render :action => 'edit' }
        format.json { respond_with_bip(@unituser) }
      end
    end
  end

I've tried several versions of unit.reload, and nothing helps. Maybe I was putting it in the wrong place?


Solution

  • I did this one sometime ago here is my code, maybe it will help you:

    Javascript:

    $(document).ready(function() {
      $('.price_bind').bind("ajax:success", function (event, data, status, xhr) {
          var parsed_data = jQuery.parseJSON(data);
          $(this).text(parsed_data.newprice);
          $(this).parentsUntil('body').find(".totalpricep span").text(parsed_data.totalprice);
      });
    }
    

    View:

    <%= best_in_place detail, :price, :classes => 'price_bind', :path => purchase_detail_path(@purchase, detail)%>
    

    Controller:

     def update
        respond_to do |format|
          if @detail.update_attributes(params[:detail])
            @[email protected]_bal
            @r=false
            if @detail.purchase != nil
              @[email protected]
            if params[:detail]['status'] && @purchase.step==1
              @remdet = @purchase.details.where(:step => 1, :status => false)
              if @remdet.empty?
                @purchase.update_attribute(:step, 2)
                @r=true
              end  
            end
            else
              @p=nil
            end
            format.html { redirect_to @detail, notice: 'Detail was successfully updated.' }
            format.json { render :json => {:newprice => @n, :totalprice => @p, :newstatus => @detail.status, :refresh => @r}}
          else
            format.html { render action: "edit" }
            format.json { render json: @detail.errors, status: :unprocessable_entity }
          end
        end
      end