加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 在验证失败后,Rails 3呈现带有嵌套属性数组

发布时间:2020-12-17 02:15:34 所属栏目:百科 来源:网络整理
导读:我有问题模型,有很多选择. 在我的问题控制器新操作中,我为我的用户创建了五个选项 def new @question = Question.new 5.times.with_index do |index| @question.options.build(:order = index) end respond_to do |format| format.html # new.html.erb forma
我有问题模型,有很多选择.

在我的问题控制器新操作中,我为我的用户创建了五个选项

def new
  @question = Question.new

  5.times.with_index do |index|
    @question.options.build(:order => index)
  end

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @question }
  end
end

在视图中,我遍历所有选项

- form_for(@question) do |f|
  .field                   
    = f.label :title,t("question.title")
    = show_errors_for(@question,:title)
    = f.text_field :title
  - @question.options.each do |option|
    - f.fields_for :options,option do |o|                                         
      .field                                                                       
        = o.label :option,t("question.option_no",{ :index => option.order })     
        = o.text_field :option                                                     
        = o.hidden_field :order,:value => option.order                            
  .actions                                                                         
    = f.submit t("add_question.create")

我的问题模型看起来像这样

class Question < ActiveRecord::Base
  attr_accessible :title,:options_attributes

  belongs_to :user
  has_many :options

  accepts_nested_attributes_for :options,:reject_if => proc { |attributes| attributes['option'].blank? }

  validates :title,:length => { :maximum => 100 },:presence => true
  validate :min_no_of_options

  def min_no_of_options
    if self.options.size < 3
      errors.add_to_base "Must have at least three options"
    end
  end
end

我的问题控制器创造了行动

def create
  if current_user
    @question = current_user.questions.build(params[:question])
  else
    @question = Question.new(params[:question])
  end

  if @question.save
    redirect_to(@question,:success => t('question.flash_success'))
  else
    flash.now[:error] = t("question.flash_error")
    render :action => "new"
  end
end

现在,当我在表单中只输入两个选项并点击创建按钮时,验证会阻止模型被保存.这很好.但是当create动作再次呈现新动作时,只会显示我填充的选项字段.留空的三个选项字段已消失.

如果我用“false”替换我的创建操作中的“@ question.save”,则行为是相同的.所以这表明我在create action中创建@question变量的方式有责任抛弃我的空选项.

但是,如果我从我的问题模型中删除:reject_if,则在失败的问题保存之后会出现空选项. (我在选项模型中对选项属性进行了状态验证)所以这告诉我在创建操作中创建@question变量的方式没有任何问题.它并没有丢掉空的选项.那么他们被踢出去了?

有一个非常相似的问题,但那里的答案并不是我想做的事情.虽然这可能是我必须要做的事情.
rails fields_for does not render after validation error on nested form

编辑

在使用rails控制台进行了一些研究之后,我注意到它确实是@question变量的创建,其中空选项被抛弃了.发生这种情况是因为我在问题模型中定义了reject_if.在从模型中注释掉reject_if之后,空选项被添加到@question变量中.

所以我想我需要删除reject_if并使用after_save回调来从数据库中销毁空选项.这样我就可以将空选项与问题一起保存,直到问题得到保存.

解决方法

我正在回答我自己的问题,因为我解决了问题.

由于“问题”模型中的reject_if,空白“选项”已从“问题”中删除.在执行以下代码时应用reject_if语句,因此删除了空白“选项”.

@question = current_user.questions.build(params[:question])

我用after_save回调替换了reject_if,删除了留空的选项.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读