Rails 3 On-site editing

I am trying to implement in-place editing in a Rails 3 application that accepts an edited value and revises the calculation based on that value. For instance:

Before editing the "loans for donation" field:
You have 10 credits left. Loans for donation: 0.

After editing the field "donation loans":
You have 5 credits left. Loans for donation: 5.

I looked at various options, and it looks like the jquery-in-place editor does what I'm looking for. For reference:

Demo works. But I think I need to make a callback function to call the ruby ​​script, so I can do some more processing. I'm not quite sure what I need to do, so I'm looking for some kind of guidance.

Here is jQuery jQuery (from /public/javascripts/application.js in the sample application) which, in my opinion, matters:

// Using a callback function to update 2 divs
$("#editme4").editInPlace({
    callback: function(original_element, html, original){
        $("#updateDiv1").html("The original html was: " + original);
        $("#updateDiv2").html("The updated text is: " + html);
        return(html);
    }
});

It seems like I need to do originaland htmlplay a little with them, and then return them to the appropriate divs. The question is, how do I do this? I think of two possibilities:

A. The jquery-in-place editor has a tool for invoking a URL. Can I use this to call a ruby ​​script and get the result? How do I do this on rails? Here is the code to call url (again from /public/javascripts/application.js); but it calls a php script.

$("#editme1").editInPlace({
    // callback: function(unused, enteredText) { return enteredText; },
    url: 'server.php',
    show_buttons: true
});

B. javascript div. ruby ​​ script, , , .

+3
2

, . , , , r-dub, , - . , - jQuery jQuery . , (B) .

, :

Available: <span id="available_cr"><%= @available_credits %></span>
Remaining: <span id="remaining_cr"><%= @remaining_credits %></span>

<%= form_for(@page, :html => { :id => 'do_fvalue' }, :remote => true) do |f| %>
  <div class="field">
    How many to buy at <%= @cost %> credits per unit? <%= f.text_field :fvalue, :autocomplete => :off %>
  </div>
  <div class="field">
    How many to buy at <%= @cost_20 %> credits per unit? <%= f.text_field :gvalue, :autocomplete => :off %>
  </div>
  <div class="actions">
    <%= f.submit "Update" %>
  </div>
<% end %>

@-variables .html.erb( .html.erb), javascript/jQuery :

<script type="text/javascript">
  var cost = parseInt(<%= @cost %>)
  var cost_20 = parseInt(<%= @cost_20 %>)
  var available_credits = parseInt(<%= @available_credits %>)
  var remaining_credits = parseInt(<%= @remaining_credits %>)
</script>

public/javascripts/applications.js :

function checkFields(){
  var to_buy_f = $("#page_fvalue").val();
  var to_buy_g = $("#page_gvalue").val();
  var subtract_f = to_buy_f * cost;
  var subtract_g = to_buy_g * cost_20;
  var remain = available_credits - subtract_f - subtract_g;
  $("#remaining_cr").text(remain);
};

$("#page_fvalue").keyup(function () {
   checkFields();
}).keyup();

$("#page_gvalue").keyup(function () {
   checkFields();
}).keyup();

checkFields #remaining_cr , #page_fvalue #page_gvalue. , _, _20, javascript-. jQuery #page_fvalue #page_gvalue keyup. , checkFields() #remaining_cr.

, "" , . , , , !

+2

, , . , , AJAX , rails html . jQuery AJAX. , .

+1

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


All Articles