Rails 3 rendering form with an array of nested attributes after failing validation -



Rails 3 rendering form with an array of nested attributes after failing validation -

i have question model has many options.

in question controller new action create 5 options ready user

def new @question = question.new 5.times.with_index |index| @question.options.build(:order => index) end respond_to |format| format.html # new.html.erb format.xml { render :xml => @question } end end

in view loop through options

- form_for(@question) |f| .field = f.label :title, t("question.title") = show_errors_for(@question, :title) = f.text_field :title - @question.options.each |option| - f.fields_for :options, alternative |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")

my question model looks this

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 @ to the lowest degree 3 options" end end end

and question controller create action

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

now, when come in 2 options in form , nail create button validation prevents model beingness saved. good. when create action renders new action again, alternative fields filled showing up. 3 alternative fields left blank have disappeared.

if replace "@question.save" in create action "false", behavior same. suggests in way create @question variable in create action responsible throwing away empty options.

but if instead remove :reject_if question model empty options showing after failing question save expected. (i have presence validation alternative attribute in alternative model) tells me there nil wrong in way create @question variable in create action. not throwing away empty options. kicked out?

there 1 pretty similar question, reply in there not do. though might have do. rails fields_for not render after validation error on nested form

edit

after more study rails console noticed creation of @question variable empty options thrown away. happens because have reject_if defined in question model. after commenting reject_if out model empty options added @question variable.

so guess need remove reject_if , utilize after_save callback destroy empty options database. way have empty options going along question until question saved.

i'm answering own question because got issue solved.

blank "options" removed "question" because of reject_if in "question" model. reject_if statement applied when code below executed, , hence blank "options" removed.

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

i replaced reject_if after_save callback removes options left blank.

ruby-on-rails-3 nested-forms

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -