2016-11-15 66 views
0

我使用Prawn生成PDF。我创建了一个基本的index视图,它可以进行一些简单的计算,并在按下提交按钮时显示答案。Ruby on Rails计算结果+对虾PDF生成器

问题是,我不知道如何采取这些答案,并将它们导出到虾。当我将它发送给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 

查看/ 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> 
+0

和calculators_path'使用哪种方法'? – inye

+0

@inye它连接到'index.pdf.prawn' – dgreen22

回答

0

我的控制器是问题。需要添加一行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 
+0

这是解决方案吗? – inye