In my application a user has to fill in their house number. This is done through the simple_form gem but everytime I click submit it says house number can't be blank. This means the field value doesn't get submitted. How come?
Schema.rb
t.integer "house_number"
Simpleform
<%= f.input :house_number %>
Model
validates :house_number, presence: true, numericality: :integer, allow_blank: false
EDIT
I removed allow_blank because it might seem redundant. It still doesn't work though. The request log is posted below.
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)
只需使用此:
validates :house_number, presence: true, numericality: :integer
查看日志,我发现我忘记了为强参数添加的house_number!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.