在我的应用程序中,用户必须填写他们的门牌号码。这是通过simple_form gem完成的,但每次我点击提交时都说门牌号不能为空。这意味着不会提交字段值。怎么样?
Schema.rb
t.integer "house_number"
Simpleform
<%= f.input :house_number %>
模型
validates :house_number, presence: true, numericality: :integer, allow_blank: false
修改
我删除了allow_blank,因为它看似多余。它仍然无法正常工作。请求日志发布在下面。
Started POST "/postcodes" for 127.0.0.1 at 2014-02-04 16:28:14 +0100
Processing by PostcodesController#create as HTML
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"0POd0L1b81uY1nSgW6b5NHLdG5KRkA0bUB4p7wwJhxk=",
"postcode"=> {"zipcode"=>"1234aa", "house_number"=>"2", "street_name"=>"", "city"=>""}, "commit"=>"Submit"}
Unpermitted parameters: house_number
(0.4ms) BEGIN
(0.3ms) ROLLBACK
Rendered postcodes/_form.html.erb (18.0ms)
Rendered postcodes/new.html.erb within layouts/application (18.7ms)
Completed 200 OK in 35ms (Views: 28.1ms | ActiveRecord: 0.7ms)
答案 0 :(得分:0)
请使用:
validates :house_number, presence: true, numericality: :integer
答案 1 :(得分:0)
看着日志,我看到我忘了添加了house_number到强大的参数!