Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6134949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:25:11+00:00 2026-05-23T17:25:11+00:00

I have question model which has many options. In my question controller new action

  • 0

I have question model which has many options.

In my question controller new action I create five options ready for my user

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

In the view I loop through all options

- 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")

My question model looks like 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 at least three options"
    end
  end
end

And my 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 I enter only two options in the form and hit the create button the validation prevents the model from being saved. Which is good. But when the create action renders the new action again, only the option fields that I filled are showing up. The three option fields which were left blank have disappeared.

If I replace the “@question.save” in my create action with “false”, the behavior is the same. So this suggests that something in the way I create the @question variable in the create action is responsible for throwing away my empty options.

But if I instead remove the :reject_if from my question model the empty options are showing up after a failing question save as expected. (I have a presence validation for the option attribute in my option model) So this tells me that there is nothing wrong in the way I create the @question variable in the create action. It is not throwing away the empty options. So where they are kicked out?

There was one pretty similar question, but the answer in there is not something I would like to do. Though it might be something I have to do.
rails fields_for does not render after validation error on nested form

EDIT

After some more study with rails console I noticed that it truly is the creation of @question variable where the empty options get thrown away. This happens because I have the reject_if defined in the question model. After commenting the reject_if out from the model the empty options were added into the @question variable.

So I guess I need to remove the reject_if and use after_save callback to destroy empty options from the database. This way I will have the empty options going along with the question until the question is saved.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:25:12+00:00Added an answer on May 23, 2026 at 5:25 pm

    I’m answering my own question because I got the issue solved.

    Blank “options” were removed from the “question” because of reject_if in the “question” model. The reject_if statement was applied when the code below was executed, and therefore blank “options” were removed.

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

    I replaced the reject_if with an after_save callback which removes the options which were left blank.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Content model which has one or many Audio files which need
A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.
I have a model called Answer which has a ForeignKey relationship to another model
I have a model Question with a field called userid , before one ask
As described in another question , I have a set of Model objects and
I have 3 models with the following associations. A model Question belongs to a
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
We have the question is there a performance difference between i++ and ++i in
We have a question with regards to XML-sig and need detail about the optional
I have a Question class: class Question { public int QuestionNumber { get; set;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.