title: Ruby on rails application - trying to convert some processing from server side to client side
body: <p>Trying to simplify oiginal question below.</p>
  
  <p>How do I use Json/Javascript/Coffeeescript to send values from the screen partial (_evaluation_assumptions.html.erb) to the assumptions in the services (services/evaluation/assumption.rb) using the apps/assets/javascripts/drills.js.coffee</p>
  
  <hr>
  
  <p>I am trying to convert some Ruby on Rails processing from server side to client side.</p>
  
  <p>Currently a user can make an estimate about the amount of oil/gas a drill will produce and then see how this effects the companies involved.  They enter their estimation amounts on a screen and then are presented with the possible results e.g he "Evaluation_Assumptions" and "Results" screen.</p>
  
  <p>I am trying to change this so both screens are on the same page and changing any of the assumptions automatically re-evaluates the possible results.</p>
  
  <p>I include below the Drills Controller, the View, parts of the evaluation module, the view partial, and what I have done so far in the App/assets/javascript file.</p>
  
  <p>I appreciate there is more than just a one or two line correction needed so any help of any kind would be appreciated - I am not expecting a "total" solution.
  - and again any help appreciated.</p>
  
  <p>thanks 
  Pierre</p>
  
  <p>the Drills Controller
  Problem 1
  The "evaluation" code the  first line produces the following run time error
  "Required parameter missing: evaluation_assumption"<br>
  I can't seem to get the evaluation assumptions into the Services module</p>
  
  <pre><code>require 'evaluation'
  
  class DrillsController &lt; ApplicationController
    before_filter :current_user
    before_filter :load_drill, except: [:index, :new, :create]
    before_filter :load_drill_interest, only: [:edit, :update, :evaluate]
    before_filter :load_resource_estimation, only: [:edit, :update, :evaluate]
    before_filter :load_user_positions, only:[:evaluate]
  
    authorize_resource
  
    rescue_from GeoPosition::Error::InvalidDegreesError,
                GeoPosition::Error::InvalidMinutesError,
                 GeoPosition::Error::InvalidSecondsError do |e|
      flash[:message] = e.message
      redirect_to :back
    end
  
    def evaluate
  
      evaluation_assumption = Evaluation.generate_projection(assumption_params)
      if evaluation_assumption.valid?
        result_list = Evaluation.generate_projection(@drill.drill_interests,
                                                     evaluation_assumption)
        respond_to do |format|
           format.json { render json: results_list}
        end
      else
        respond_to do |format|
          format.json { render json: []}
        end
      end
  
    end
  </code></pre>
  
  <p>The Services code which does the evaluation</p>
  
  <p>Services
    Evaluation
      assumption.rb (see below)
      calculate_results.rb
      result.rb
    evaluation.rb (see below)</p>
  
  <p>Evaluation/assumption.rb</p>
  
  <pre><code>class Evaluation
    class Assumption
  
      include ActiveAttr::Model
  
      attribute :resource_estimation_oil, type: Float
      attribute :in_ground_value_oil, type: Float
      attribute :resource_estimation_gas, type: Float
      attribute :in_ground_value_gas, type: Float
      attribute :probability_of_success, type: Integer
  
      validates :resource_estimation_oil, presence: true, 
                :numericality =&gt; {:greater_than_or_equal_to =&gt; 0}
      validates :in_ground_value_oil, presence: true,
                :numericality =&gt; {:greater_than =&gt; 0}
      validates :resource_estimation_gas, presence: true,
                :numericality =&gt; {:greater_than_or_equal_to =&gt; 0}
      validates :in_ground_value_gas, presence: true,
                :numericality =&gt; {:greater_than =&gt; 0}
      validates :probability_of_success, presence: true, 
                :numericality =&gt; {:greater_than =&gt; 0, :less_than_or_equal_to =&gt; 100 }
  
    end
  end
  </code></pre>
  
  <p>evaluation.rb</p>
  
  <pre><code>require_relative './evaluation/assumption'
  require_relative './evaluation/calculate_results'
  require_relative './evaluation/result'
  
  class Evaluation
      def self.generate_projection(drill_interests, assumption_params)
      result_list = []
  
      evaluation_assumption = Assumption.new(assumption_params)
      if evaluation_assumption.valid?
          drill_interests.each do |drill_interest|
            listings = drill_interest.company.company_listings
            result_list += CalculateResults.new(drill_interest,listings,evaluation_assumption).results
         end
         result_list.sort_by! {|u| u.leverage}
      end
      result_list
    end
  end
  </code></pre>
  
  <p>The screens (main screen and partials for evaluation assumptions and results.</p>
  
  <p>evaluate.html.erb</p>
  
  <pre><code>&lt;% content_for :title, "Drill Investor - Valuations" %&gt;
  &lt;% content_for :tab_group, "drills" %&gt;
  &lt;% content_for :tab_id, 'Investor Room' %&gt;
  &lt;%= content_tag :div, class: "breadcrumbs" do %&gt;
     &lt;%= link_to 'Drills', drill_stakes_path %&gt; &amp;raquo; 
    &lt;%= @drill.name %&gt;
  &lt;% end %&gt;
  
  &lt;section&gt;
    &lt;article class="single"&gt;
      &lt;div&gt;
        &lt;span&gt;valuation&lt;/span&gt;
        &lt;select name="valuation" id="valuation" size="1"&gt;
          &lt;option value="option1"&gt;Default&lt;/option&gt;
          &lt;option value="option1"&gt;User Save Name&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
      &lt;div class="form"&gt;
        &lt;%= render 'evaluation_assumptions' %&gt;
      &lt;/div&gt;
      &lt;div class="form"&gt;
        &lt;%= render 'evaluation_results' %&gt; 
      &lt;/div&gt;
  
      &lt;div class="form"&gt;
        &lt;%= render 'user_positions/display_multiple' %&gt;
      &lt;/div&gt;
  
    &lt;/article&gt;
  &lt;/section&gt;
  </code></pre>
  
  <p>the partial for evaluation_assumptions
  _evaluation_assumptions.htnl.erb</p>
  
  <pre><code>&lt;%= simple_form_for :evaluation_assumption,
        :html =&gt; { class: 'infogroup' },
        :validate =&gt; true do |f| %&gt;   
    &lt;%= content_tag :header, "Assumptions", class: 'infogroup-header'%&gt;  
    &lt;%= content_tag :div, class: 'infogroup-body' do %&gt; 
  
      &lt;%= content_tag :table, class: 'info', border: 0, cellspacing: 0, cellpadding: 0 do %&gt;    
        &lt;%= f.hidden_field :drill_id %&gt;
        &lt;%= f.hidden_field :member_profile_id %&gt;
        &lt;tr&gt;   
        &lt;%= content_tag :td, content_tag(:span, "Target"), class: 'column' %&gt;  
        &lt;%= content_tag :td, content_tag(:span, ""), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, "Discovery Target"), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, "Potential Resource size"), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, ""), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, ""), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, "In-Ground Value"), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, "Probability of Success"), class: 'column' %&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
        &lt;%= content_tag :td, content_tag(:span, @temp_var,  
                    class: "general ralign"), class: 'column' %&gt;
  
        &lt;%= content_tag :td, content_tag(:span, ""), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, "Oil"), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, "p50"), class: 'column' %&gt;
        &lt;%= content_tag :td, f.text_field(:resource_estimation_oil,  
                    class: "general ralign"), class: 'data' %&gt;
        &lt;%= content_tag :td, content_tag(:span, "mmbbl"), class: 'column' %&gt;
        &lt;%= content_tag :td, f.text_field(:in_ground_value_oil, 
                    class: "general ralign"), class: 'data' %&gt;
        &lt;%= content_tag :td, f.text_field(:probability_of_success, 
                    class: "general ralign"), class: 'data' %&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
        &lt;%= content_tag :td, content_tag(:span, ""), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, ""), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, "Gas"), class: 'column' %&gt;
        &lt;%= content_tag :td, content_tag(:span, "p50"), class: 'column' %&gt;
        &lt;%= content_tag :td, f.text_field(:resource_estimation_gas, 
                    class: "general ralign"), class: 'data' %&gt;
        &lt;%= content_tag :td, content_tag(:span, "bcf"), class: 'column' %&gt;
        &lt;%= content_tag :td, f.text_field(:in_ground_value_gas, 
                    class: "general ralign"), class: 'data' %&gt;
        &lt;/tr&gt;
      &lt;% end %&gt;   
  ...
  </code></pre>
  
  <p>the results screen
  _evaluation_results.html.erb</p>
  
  <pre><code>&lt;%= simple_form_for :evaluation_assumption,
        :html =&gt; { class: 'infogroup', :method =&gt; :post } do |f| %&gt;
    &lt;%= content_tag :header, "Results", class: 'infogroup-header'%&gt;  
    &lt;%= content_tag :div, class: 'infogroup-body' do %&gt; 
      &lt;%= content_tag :table, class: 'info', border: 0, cellspacing: 5, cellpadding: 0 do %&gt;     
        &lt;tr&gt;
          &lt;th&gt;Company&lt;/th&gt;
          &lt;th&gt;SP&lt;/th&gt;
          &lt;th&gt;Mkt Cap&lt;/th&gt;
          &lt;th&gt;Discovery Value&lt;/th&gt;
          &lt;th&gt;DV/Share&lt;/th&gt;
          &lt;th&gt;Leverage&lt;/th&gt;
          &lt;th&gt;Risked Lev &lt;/th&gt;
          &lt;th&gt;Leverage with CFD's&lt;/th&gt;
        &lt;/tr&gt;
        &lt;% @result_list.each do |result| %&gt;                 
          &lt;tr&gt;
             &lt;td&gt;&lt;%= result.listing_name %&gt;&lt;/td&gt;
             &lt;td&gt;&lt;%= number_to_currency(result.listing.share_price_dollars,
                    :precision =&gt; 3, :delimiter =&gt; ',')     %&gt;&lt;/td&gt;
  …
  </code></pre>
  
  <p>The javascript
  Here I can get data to the screen (currently just using hard coded data as opposed to getting data from database).  What I can't do is put this data into the Services Assumptions.  How do I do this ?
  drills.js.coffee</p>
  
  <pre><code>loadEvaluationResults = (drill_id)-&gt;
    $.getJSON "/drills/#{drill_id}/evaluate.js", $('#evaluation_assumption_params').serialize(),   (data,status,xhr)-&gt;
    # ('/drills/1/evaluate.json',{type:"post", data: $('#evaluation_assumption_params').serialize()});
    # fill in table
    #    load new page from drills - evaluation
  
  $('document').ready -&gt;
    if $('#evaluation_assumption_params').length == 1  
       # load in some assumptions - this will be replaced by getting correct assumptions
       $('#assumption_params_resource_estimation_oil').val('1.106')
       $('#assumption_params_in_ground_value_oil').val('4')
       $('#assumption_params_resource_estimation_gas').val('0')
       $('#assumption_params_in_ground_value_gas').val('1')
       $('#assumption_params_probability_of_success').val('20')      
       # this not working to get standard values for this drill
     $('#evaluation_assumption_params').change((event) -&gt;
           assid = $('#valuation').val()
           loadAssumption assid
       )
  </code></pre>