我对 ruby on rails 很陌生。我正在尝试开发一个仅在控制器中使用 ActiveModel 对象而不保存的应用程序。不幸的是,单击提交按钮后,我收到错误消息。这个应用程序的目的是执行一些计算并显示结果。我怎样才能做到这一点?
路由错误没有路由匹配 [POST] "/shugarcalc"
应用程序/模型/sugar_amount.rb
class SugarAmount
include ActiveModel::Model
attr_accessor :l, :h, :b, :tw, :tf
VALID_NUMBER_REGEX= /[0-9]{1,2}([,][0-9]{1,2})?/
validates :l, presence: true, format: { with: VALID_NUMBER_REGEX }, length: { maximum: 10 }
validates :h, presence: true, format: { with: VALID_NUMBER_REGEX }, length: { maximum: 10 }
end
配置/路由.rb
SimpleDesign::Application.routes.draw do
root 'static_pages#home'
resources :sugar_amount, only: [:new, :create]
match '/shugarcalc', to: 'shugar_amounts#shugar', via: 'get'
end
应用程序/控制器/shugar_amounts_controller.rb
class ShugarAmountsController < ApplicationController
def sugar
@sugar_amount=SugarAmount.new
end
def create
@sugar_amount = SugarAmount.new(params[:sugar_amount])
/here i want to use some functions /
redirect_to root_url
/this is temporary, just to see if anything happens /
end
end
应用程序/views/sugar_amounts/sugar.html.erb
<%= form_for(@sugar_amount) do |f| %>
<%= f.label :l, "eggs" %>
<%= f.text_field :l %><br>
<%= f.label :h, "flour [mm]" %>
<%= f.text_field :h %><br>
<%= f.submit "Reduce!" %>
<% end %>