【发布时间】:2013-12-22 05:07:32
【问题描述】:
我正在尝试使用 form_tag 构建一个用餐小费计算器,但我的表单提交会产生路由错误。我花了一个小时搜索,但无法弄清楚。
错误:没有路由匹配 [POST] "/tips"
我的路线
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
【问题讨论】:
标签: ruby-on-rails routing