Rails3 js response after json update

using http://blog.bernatfarrero.com/in-place-editing-with-javascript-jquery-and-rails-3/

Gem uses json to update, but how can I get update.js.erb to update different parts of my pages?

EDIT Using this on the invoice page. Each item in the invoice has a price field that can be updated using best_in_place.

I need to update the total price for a position and the amount due due to the invoice only after a successful field update.

Finished with something like:

respond_to do |format| if @item.update_attributes(params[:item]) format.html { redirect_to order_url(@item.order_id), :notice => "Successfully updated item." } format.js { } format.json { head :ok } 

Edited line best_in_place.js # 175

 loadSuccessCallback : function(data) { this.element.html(data[this.objectName]); // Binding back after being clicked $(this.activator).bind('click', {editor: this}, this.clickHandler); if(this.objectName == "item") $.getScript('update.js'); // If its an item, call update.js.erb }, 
+4
source share
3 answers

You do not need to view update.js.erb. As ezkl already pointed out, you need to set the response_to of your controller to json. In addition, you need to activate the best in place in your public / javascripts/application.js

 $(document).ready(function() { jQuery(".best_in_place").best_in_place() }); 

and add it to your view:

  <td><%= best_in_place task, :name, :type => :input, :nil => "Click to edit" %></td> 

I have an Ajax tutorial with Prototype and JQuery on my own site - and the JQuery Part uses the best in place: http://www.communityguides.eu/articles/15

0
source

I wanted to do the same with best_in_place pcasa. In the current version (1.0.2), the loadSuccessCallback function fires the ajax: success event. This allows you to do this in the application.js application:

 $('.best_in_place') .best_in_place() .bind('ajax:success', function(e) { // do your post-json call magic }); 
+6
source

Have you tried something like:

 def update @user = User.find(params[:id]) respond_to do |format| if @user.update_attributes(params[:user]) format.html { redirect_to(@user, :notice => 'User was successfully updated.') } format.json { head :ok } format.js else format.html { render :action => "edit" } format.json { render :json => @user.errors, :status => :unprocessable_entity } end end end 

I did not test this with code in the tutorial, but the idea is that you have to say that the controller method loads the Javascript file. I'm not sure how this will interact with the redirect, but the principle is correct.

0
source

Source: https://habr.com/ru/post/1348013/


All Articles