Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The solution to this problem is quite complex, but I have found a way to do it. It is surprising to me that the solution is not readily available, since it does seem, as r-dub says, something that is quite common. I ended up not using any of the edit-in-place facilities of a jQuery plugin, and instead modified a text field directly with the help of jQuery on the basis of a form text field. I other words, I followed (B) above.</p> <p>I have a view that looks like this:</p> <pre><code>Available: &lt;span id="available_cr"&gt;&lt;%= @available_credits %&gt;&lt;/span&gt; Remaining: &lt;span id="remaining_cr"&gt;&lt;%= @remaining_credits %&gt;&lt;/span&gt; &lt;%= form_for(@page, :html =&gt; { :id =&gt; 'do_fvalue' }, :remote =&gt; true) do |f| %&gt; &lt;div class="field"&gt; How many to buy at &lt;%= @cost %&gt; credits per unit? &lt;%= f.text_field :fvalue, :autocomplete =&gt; :off %&gt; &lt;/div&gt; &lt;div class="field"&gt; How many to buy at &lt;%= @cost_20 %&gt; credits per unit? &lt;%= f.text_field :gvalue, :autocomplete =&gt; :off %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit "Update" %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>I then stuff the rails @-variables into a .html.erb partial (rendered at the beginning of my page .html.erb) so that javascript/jQuery can access them:</p> <pre><code>&lt;script type="text/javascript"&gt; var cost = parseInt(&lt;%= @cost %&gt;) var cost_20 = parseInt(&lt;%= @cost_20 %&gt;) var available_credits = parseInt(&lt;%= @available_credits %&gt;) var remaining_credits = parseInt(&lt;%= @remaining_credits %&gt;) &lt;/script&gt; </code></pre> <p>Then, in public/javascripts/applications.js, I have the following code:</p> <pre><code>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(); </code></pre> <p>The checkFields function updates #remaining_cr based on the values found in #page_fvalue and #page_gvalue. Notice that it makes use of available_credits, cost, and cost_20, which we stuffed into the javascript variables above. The last two functions tell jQuery to monitor #page_fvalue and #page_gvalue for keyup events. When the user presses a key, it calls checkFields() to update #remaining_cr.</p> <p>All of this aside, when the user clicks "submit" on the form, the whole form gets submitted with the current values. I don't know whether this is the most efficient way to do what I was trying to do, but it works!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload