试图简化下面的原始问题。
如何使用 Json/Javascript/Coffeescript 将值从屏幕部分 (_evaluation_assumptions.html.erb) 发送到使用应用程序/资产/javascripts/drills.js 的服务中的假设 (services/evaluation/assumption.rb)。咖啡
我正在尝试将一些 Ruby on Rails 处理从服务器端转换为客户端。
目前,用户可以估计钻机将产生的石油/天然气量,然后查看这对所涉及的公司有何影响。他们在屏幕上输入估计金额,然后显示可能的结果,例如“Evaluation_Assumptions”和“结果”屏幕。
我正在尝试更改这一点,以便两个屏幕都在同一页面上,并且更改任何假设都会自动重新评估可能的结果。
我在下面包含了 Drills 控制器、视图、评估模块的部分、部分视图以及我目前在 App/assets/javascript 文件中所做的工作。
我很感激不仅需要一两行更正,因此任何形式的帮助都将不胜感激——我并不期待“全面”的解决方案。- 再次感谢任何帮助。
谢谢皮埃尔
Drills Controller 问题 1 第一行的“评估”代码产生以下运行时错误“缺少必需的参数:评估假设”
我似乎无法将评估假设放入服务模块
require 'evaluation'
class DrillsController < 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
进行评估的服务代码
服务评估假设.rb(见下文) calculate_results.rb 结果.rb 评估.rb(见下文)
评估/假设.rb
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 => {:greater_than_or_equal_to => 0}
validates :in_ground_value_oil, presence: true,
:numericality => {:greater_than => 0}
validates :resource_estimation_gas, presence: true,
:numericality => {:greater_than_or_equal_to => 0}
validates :in_ground_value_gas, presence: true,
:numericality => {:greater_than => 0}
validates :probability_of_success, presence: true,
:numericality => {:greater_than => 0, :less_than_or_equal_to => 100 }
end
end
评估.rb
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
屏幕(用于评估假设和结果的主屏幕和部分屏幕。
评估.html.erb
<% content_for :title, "Drill Investor - Valuations" %>
<% content_for :tab_group, "drills" %>
<% content_for :tab_id, 'Investor Room' %>
<%= content_tag :div, class: "breadcrumbs" do %>
<%= link_to 'Drills', drill_stakes_path %> »
<%= @drill.name %>
<% end %>
<section>
<article class="single">
<div>
<span>valuation</span>
<select name="valuation" id="valuation" size="1">
<option value="option1">Default</option>
<option value="option1">User Save Name</option>
</select>
</div>
<div class="form">
<%= render 'evaluation_assumptions' %>
</div>
<div class="form">
<%= render 'evaluation_results' %>
</div>
<div class="form">
<%= render 'user_positions/display_multiple' %>
</div>
</article>
</section>
部分评估假设 _evaluation_assumptions.htnl.erb
<%= simple_form_for :evaluation_assumption,
:html => { class: 'infogroup' },
:validate => true do |f| %>
<%= content_tag :header, "Assumptions", class: 'infogroup-header'%>
<%= content_tag :div, class: 'infogroup-body' do %>
<%= content_tag :table, class: 'info', border: 0, cellspacing: 0, cellpadding: 0 do %>
<%= f.hidden_field :drill_id %>
<%= f.hidden_field :member_profile_id %>
<tr>
<%= content_tag :td, content_tag(:span, "Target"), class: 'column' %>
<%= content_tag :td, content_tag(:span, ""), class: 'column' %>
<%= content_tag :td, content_tag(:span, "Discovery Target"), class: 'column' %>
<%= content_tag :td, content_tag(:span, "Potential Resource size"), class: 'column' %>
<%= content_tag :td, content_tag(:span, ""), class: 'column' %>
<%= content_tag :td, content_tag(:span, ""), class: 'column' %>
<%= content_tag :td, content_tag(:span, "In-Ground Value"), class: 'column' %>
<%= content_tag :td, content_tag(:span, "Probability of Success"), class: 'column' %>
</tr>
<tr>
<%= content_tag :td, content_tag(:span, @temp_var,
class: "general ralign"), class: 'column' %>
<%= content_tag :td, content_tag(:span, ""), class: 'column' %>
<%= content_tag :td, content_tag(:span, "Oil"), class: 'column' %>
<%= content_tag :td, content_tag(:span, "p50"), class: 'column' %>
<%= content_tag :td, f.text_field(:resource_estimation_oil,
class: "general ralign"), class: 'data' %>
<%= content_tag :td, content_tag(:span, "mmbbl"), class: 'column' %>
<%= content_tag :td, f.text_field(:in_ground_value_oil,
class: "general ralign"), class: 'data' %>
<%= content_tag :td, f.text_field(:probability_of_success,
class: "general ralign"), class: 'data' %>
</tr>
<tr>
<%= content_tag :td, content_tag(:span, ""), class: 'column' %>
<%= content_tag :td, content_tag(:span, ""), class: 'column' %>
<%= content_tag :td, content_tag(:span, "Gas"), class: 'column' %>
<%= content_tag :td, content_tag(:span, "p50"), class: 'column' %>
<%= content_tag :td, f.text_field(:resource_estimation_gas,
class: "general ralign"), class: 'data' %>
<%= content_tag :td, content_tag(:span, "bcf"), class: 'column' %>
<%= content_tag :td, f.text_field(:in_ground_value_gas,
class: "general ralign"), class: 'data' %>
</tr>
<% end %>
...
结果屏幕 _evaluation_results.html.erb
<%= simple_form_for :evaluation_assumption,
:html => { class: 'infogroup', :method => :post } do |f| %>
<%= content_tag :header, "Results", class: 'infogroup-header'%>
<%= content_tag :div, class: 'infogroup-body' do %>
<%= content_tag :table, class: 'info', border: 0, cellspacing: 5, cellpadding: 0 do %>
<tr>
<th>Company</th>
<th>SP</th>
<th>Mkt Cap</th>
<th>Discovery Value</th>
<th>DV/Share</th>
<th>Leverage</th>
<th>Risked Lev </th>
<th>Leverage with CFD's</th>
</tr>
<% @result_list.each do |result| %>
<tr>
<td><%= result.listing_name %></td>
<td><%= number_to_currency(result.listing.share_price_dollars,
:precision => 3, :delimiter => ',') %></td>
…
javascript 在这里我可以将数据显示到屏幕上(目前仅使用硬编码数据,而不是从数据库中获取数据)。我不能做的是将这些数据放入服务假设中。我该怎么做呢 ?Drills.js.coffee
loadEvaluationResults = (drill_id)->
$.getJSON "/drills/#{drill_id}/evaluate.js", $('#evaluation_assumption_params').serialize(), (data,status,xhr)->
# ('/drills/1/evaluate.json',{type:"post", data: $('#evaluation_assumption_params').serialize()});
# fill in table
# load new page from drills - evaluation
$('document').ready ->
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) ->
assid = $('#valuation').val()
loadAssumption assid
)