Ruby上Rails计算结果+大虾PDF生成器
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
我正在使用 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