首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby on Rails -在某些字段上验证失败后丢失表单值

Ruby on Rails -在某些字段上验证失败后丢失表单值
EN

Stack Overflow用户
提问于 2020-10-23 17:00:06
回答 2查看 96关注 0票数 0

我有一张表格

代码语言:javascript
复制
     <%= 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()"} %>

和控制器

代码语言:javascript
复制
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

和型号:

代码语言:javascript
复制
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 }

问题是:用于教学字段的problem teaching、non_publicly_funded_teaching和support_for_teaching在验证后不会保留这些值。电子邮件,姓氏和名字。

请告诉我如何解决这个问题。我猜这与这些字段的value属性有关?

非常感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-24 00:57:56

看看如何在:publiclyfundedteaching:non_publicly_funded_teaching的表单字段中使用:value => '0' (您提到的第三个字段不在表单中)。即使您重新加载以前填写的表单,它们也会将字段中的值设置为0。您可以删除它或尝试添加一个if语句,如下所示(假设提交是模型):

代码语言:javascript
复制
<%= f.input :publiclyfundedteaching, label: "Publicly funded teaching (PFT)", input_html: { style: 'width:7%', id: "publiclyfundedteaching", value: @submission.publiclyfundedteaching || 0, onkeyup: "myFunction()" } %>
票数 0
EN

Stack Overflow用户

发布于 2020-10-23 18:01:21

submission_params中,你可能有类似这样的东西:

代码语言:javascript
复制
def submission_params
  params.require(:your_model_name).permit(:firstname, :surname, :email)
end

这将删除任何其他字段,以保护它们不受客户端伪造数据的影响

如果希望字段为可写字段,请将其添加到允许

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64497018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档