2017-01-17 14 views
1

この簡単なBMI計算ウェブプログラムですGETとPOSTの両方で値を渡す方法は?

enter image description here enter image description here

BMI_controller.rb

class BmiController < ApplicationController 
     def enter 
    end 

    def calculate 
    @height = params[:height].to_f 
    @weight = params[:weight].to_f 
    @bmi=(@weight/(@height*@height)) 
    @category 

    if @bmi<18.5 
     @category= "Underweight" 

    end 
    if @bmi>18.5 and @bmi<23 
     @category="Normal" 

    end 

if @bmi>23 and @bmi<25 
    @category= "Overweight" 

    end 


    if @bmi>25 
    @category="Obese" 

    end 


end 
end 

calculate.html.erb

<p>Height: <%= @height %></p> 
<p>Weight: <%= @weight %></p> 
<p>BMI: <%= @bmi %></p> 
<p>Category: <%= @category %></p> 

Enter_data.html.erb

`<h1>Welcome to BMI Calculator</h1> 

<form action="http://localhost:3000/bmi/[email protected]&[email protected]" > 
<p>Height: <input type="text" name="height"></p> 
<p>Weight: <input type="text" name="weight"></p> 
    <br><br> 
    <input type="submit" namevalue="Calculate" > 
</form>` 

私は私が使用しての方法は、それがGETと呼ばれている次のページに@heightと@weight過去にこの方法を使用して

`<form action="http://localhost:3000/bmi/[email protected]&[email protected]"` > 

を計算しますか? しかし、それぞれ標準method="get"method="post"を行う方法は?

更新

しかし、私はあまりにも<form action="http://localhost:3000/bmi/calculate?height=1&weight=23212" >または<form action="Calculate" >//<<=this should refer back to the function- calculate仕事を見つけました。

誰でも説明できますか?

答えて

0

私はあなたの質問をはっきりと理解していますが、私はあなたが混乱するかもしれないと思ういくつかのことを説明しようとします。

<form method="get" action="http://example.com/action">を使用すると、GETリクエストが送信され、URLのフォーム要素が表示されます。 http://example.com/action?height=72&weight=90

ご存知のように、method="post"はリクエストをPOSTします。同じ操作を使用している場合(例: http://example.com/actionの場合は、GETとPOSTの両方を処理していることを確認する必要があります。

config/routes.rbファイルでは、POSTリクエストを処理できることを確認する必要があります。あなたは読む必要がありますhttp://guides.rubyonrails.org/routing.html

0

レールを使用しているので、私はヘルパーメソッドform_tagをhtmlで書く代わりに使用します。デフォルトでは、フォームの送信は、POSTリクエストと見なされていますが、オプションを渡すことにより、他のHTTP動詞を使用することができます:メソッドのparam:

例:form_tag(<path>, method: :get)

パスがであなたのroutes.rbをファイルに定義する必要があります正しいhttp動詞。

関連する問題