2017-01-17 3 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>` 

나는 다음 페이지로 @height 및 @weight 과거에이 방법을 사용하고

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

GET라고 내가 사용하는 방법을 계산? 그러나 각각 표준 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"는 요청을 게시 할 예정입니다. 동일한 작업 (예 : http://example.com/action의 경우 GET 및 POST를 모두 처리해야합니다. 당신의 설정/routes.rb 파일에서

당신은 POST 요청을 처리 할 수 ​​있는지 확인하는 것이 좋습니다. 읽어야합니다 http://guides.rubyonrails.org/routing.html

0

레일을 사용하고 있기 때문에 html로 작성하는 대신 보조 방법 form_tag을 사용합니다. 기본적으로 양식 제출는 POST 요청으로 간주되지만 옵션으로 전달하여 다른 HTTP 동사를 사용할 수 있습니다 방법 PARAM을 :

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

경로가와 routes.rb 파일에 정의되어야한다 올바른 http 동사.

관련 문제