【发布时间】:2021-02-06 08:13:20
【问题描述】:
我有一个表格
<%= f.input :firstname, :label => "Forename", :input_html => { :placeholder => "Forename"} %>
<%= f.input :surname, :label => "Surname", :input_html => { :placeholder => "Surname", :"data-
<%= f.input :publiclyfundedteaching, :label => "Publicly funded teaching (PFT)", :input_html => {:style => 'width:7%', :id => "publiclyfundedteaching", :value => '0', :onkeyup => "myFunction()"} %>
<%= f.input :non_publicly_funded_teaching, :label => "Non-Publicly Funded teaching (NPFT)", :input_html => {:style => 'width:7%', :id => "non_publicly_funded_teaching", :value => '0', :onkeyup => "myFunction()"} %>
和控制器
def create
@submission = Submission.new(submission_params)
@email = @submission.email
puts @email
if @submission.save
flash[:notice] = 'Your user was successfully created.'
else
@staffform = Form.find(10000)
@livestatus = @staffform.status
puts @staffform.status
puts "not saved"
puts @submission.errors.full_messages
render :new
end
和型号:
validates :firstname, presence: true
validates :surname, presence: true
validates :email, presence: true
validates :total, numericality: { only_integer: true, equal_to: 100}
# First group
validates :publiclyfundedteaching, numericality: { only_integer: true }
validates :non_publicly_funded_teaching, numericality: { only_integer: true }
validates :support_for_teaching, numericality: { only_integer: true }
问题是 :publiclyfundedteaching、non_publicly_funded_teaching 和 support_for_teaching 的教学字段在验证后不保留这些值。电子邮件、姓氏和名字都可以。
请告诉我如何解决这个问题。 我猜这与这些字段的 value 属性有关?
非常感谢!!
【问题讨论】:
-
您能告诉我们submission_params 在创建新记录之前是什么样子的吗?选项
only_integer: true根据正则表达式检查字段 /\A[+-]?\d+\z/ 请注意,“2”会匹配,但“2.0”不会。 -
感谢您的回复,请参阅下面的评论