2016-11-15 10 views
0

私はPrawnを使ってPDFを生成しています。私は基本的なindexビューを作成しました。このビューでは、単純な計算が行われ、サブミットボタンが押されたときの回答が表示されます。Ruby on Railsの計算結果+ Prawn PDF Generator

問題は、私はこれらの答えを取って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 

ビュー/ 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

関連する問題