[英]Ruby on Rails calculation results + Prawn PDF Generator
我正在使用Prawn
生成PDF。 我創建了一個基本的index
視圖,該視圖可以進行一些簡單的計算並在按下“提交”按鈕時顯示答案。
問題是,我不知道如何獲得這些答案並將其導出到Prawn。 當我將其發送給Prawn時,它會將輸入到計算器中的值重置為零,因此無論顯示什么,都將顯示相同的值,並且不會顯示計算出的答案。 下面的MVC。 真的感謝任何幫助。
模型
class Calculator < ApplicationRecord
def self.to_celsius(fahrenheit)
(fahrenheit.to_i - 30) / 2
end
def self.square_foot(a,b)
a.to_i * b.to_i
end
end
控制者
class CalculatorsController < ApplicationController
def new
@result = Calculator.to_celsius(params[:fahrenheit])
@square = Calculator.square_foot(params[:length], params[:width])
end
def index
@result = Calculator.to_celsius(params[:fahrenheit])
@square = Calculator.square_foot(params[:length], params[:width])
end
end
Views / index.html.erb
<div class="row">
<div class="col-md-8 col-md-offset-2 calculations">
<%= form_for :calculators, url: { action: :new },method: :get do |f| %>
<p> Convert Celsius to Fahrenheit: </p>
<%= label_tag :fahrenheit, "Enter Fahrenheit", class: "input-a" %>
<%= text_field_tag :fahrenheit, params[:fahrenheit] %>
<p> Square Footage Calculator: </p>
<%= label_tag :length, "Enter length ft", class: "input-a" %>
<%= text_field_tag :length, params[:length] %>
<%= label_tag :width, "Enter width ft", class: "input-a" %>
<%= text_field_tag :width, params[:width] %>
<%= f.submit 'Calculate!' %>
<% end %>
<% unless @result.nil? %>
This is <p> = <%= @result %> degrees celsius! </p>
<% end %>
<p><%= link_to "Create PDF", calculators_path(:format => 'pdf') %></p>
</div>
</div>
我的控制器是問題所在。 需要添加一行以render :index
因此,新的控制器是:
class CalculatorsController < ApplicationController
def new
@result = Calculator.to_celsius(params[:fahrenheit])
@square = Calculator.square_foot(params[:length], params[:width])
render :index
end
def index
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.