0
form_tag를 사용하여 식사 팁 계산기를 만들려고하고 양식 제출이 라우팅 오류를 생성합니다. 나는 1 시간의 검색에 썼다. 그러나 그것을 이해할 수 없다.라우팅 오류 : 경로가 [POST]와 일치하지 않습니다.
오류 : 경로와 일치 [POST] "/ 팁"
내 경로
Tips::Application.routes.draw do
root "tips#calculate"
get "tips/calculate"
post "tips/calculate"
end
컨트롤러 :
class TipsController < ApplicationController
def calculate
@tip = params[:price].to_i * params[:tip].to_i
render 'calculate'
end
end
보기 :
<h1>Enter your meal info</h1>
<p>Find me in app/views/tips/calculate.html.erb</p>
<%= form_tag('/tips#calculate') do %>
<p>
<%= label_tag("Meal price:") %>
<%= text_field_tag(:price, nil, placeholder: "Enter cost without tax and tip")%>
</p>
<p>
<%= label_tag("Tip:") %>
<%= text_field_tag(:tip, nil, placeholder: "Enter tip (i.e. 15 for 15%)") %>
</p>
<p>
<%= submit_tag 'Calculate!' %>
</p>
<p>
<%= label_tag("Tip value:") %>
<%= text_field_tag("tip", @tip, precision: 2, :readonly => true) %> #this probably needs to be changed
<% end %>
레이크 경로 :
$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET/ tips#calculate
tips_calculate GET /tips/calculate(.:format) tips#calculate
POST /tips/calculate(.:format) tips#calculate
아마도 혼란은'# '의 다른 의미에서 유래했을 것입니다. 경로 목록에서'tips # calculate'는'TipsController' 클래스의'calculate' 메소드를 의미하지만, URL의'#'은 조각입니다. –
예, 해결되었습니다! 건배는 너무 새롭고, upvote 수 없습니다. 관련 노트에서, http://guides.rubyonrails.org/form_helpers.html에 따르면'form_tag'은'<% = form_tag ('form_calculate_path') do %>'일 수 있어야하지만 또한 오류를 던집니다. 누구든지 (더 나은) form_tag 예제/워드 프로세서를 알고 있습니까? – user2936314
'form_tag (tips_calculate_path) ... '를 사용할 수 있어야합니다. 'rake routes'의 출력에'prefix' 컬럼이 보입니까? 그 이름에'_path' 또는'_url'을 추가하여 경로 (root ... 보통 /) 또는 url (http://example.com/path/goes/here와 같이)을 얻을 수 있습니다. – Zajn