Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3: validation, presence: true is only validating text field but not radio button
    primarykey
    data
    text
    <p>So I'm working on this part of a rails app with nested forms. I am having trouble with getting validation to work. So the parent form is a model to store Questions and the child form is a model to store Answers. </p> <p>There are 3 different types of questions: number (text field), yes/no (radio buttons), agree/disagree (radio buttons). </p> <p>I have a simple validation in the answers model: <code>validates :value, presence: true</code></p> <p>So for example I create a question of type number, it generates a text field and if I submit it as empty the validation works and the errors are rendered on the page. However, if I pick one of the the other 2 options, which are both radio buttons, I can submit the form without making a selection and the validation doesn't work. I noticed in the console that only the question is inserted into the database, but the answer is not (with the radio button form); normally I would assume that at least there would be nil values passed, but the INSERT query doesn't even show up.</p> <p>I cheated a little by having a hidden field in the radio button forms, and creating a change handler that sets the value of the radio button to the hidden field whenever the radio button selected is changed. However, I would really like to dig deeper and figure out the real issue, because it's always good to have a back-up in case javascript is disabled.</p> <p><strong>Answer Model</strong></p> <pre><code>class Answer &lt; ActiveRecord::Base attr_accessible :value, :user_id, :meter_id, :question_id belongs_to :user belongs_to :question validates :value, presence: true, :numericality =&gt; true before_save :associate_with_meter_id before_save :associate_with_user_id def associate_with_meter_id self.meter_id = question.user.meter_id end def associate_with_user_id self.user_id = question.user.id end end </code></pre> <p><strong>Question Model</strong></p> <pre><code>class Question &lt; ActiveRecord::Base attr_accessible :description, :taxonomy, :user_id, :answers_attributes belongs_to :user has_many :answers accepts_nested_attributes_for :answers validates :description, presence: { :on =&gt; :create } validates :taxonomy, presence: { :on =&gt; :create } def relevance_score rand end end </code></pre> <p><strong>Log</strong></p> <pre><code>Started POST "/questions" for 127.0.0.1 at 2012-06-12 09:21:25 -0400 Processing by QuestionsController#create as HTML Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"knwvfB6q6Q7qoTprc/3R4Et3r13xWzpAB1Iq8FsRndQ=", "question"=&gt;{"description"=&gt;"How are you?", "taxonomy"=&gt;"yesno"}, "submit_button"=&gt;"Ask"} User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 585460615 LIMIT 1 SQL (0.1ms) BEGIN SQL (4.3ms) INSERT INTO `questions` (`avganswer`, `coeff`, `created_at`, `description`, `pval`, `quality`, `rank`, `responses`, `rsquare`, `skips`, `taxonomy`, `updated_at`, `user_id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["avganswer", nil], ["coeff", nil], ["created_at", Tue, 12 Jun 2012 13:21:25 UTC +00:00], ["description", "How are you?"], ["pval", 0.0], ["quality", 0.0], ["rank", nil], ["responses", nil], ["rsquare", 0.0], ["skips", nil], ["taxonomy", "yesno"], ["updated_at", Tue, 12 Jun 2012 13:21:25 UTC +00:00], ["user_id", 585460615]] (0.3ms) COMMIT SQL (0.0ms) BEGIN (0.0ms) COMMIT Redirected to http://localhost:3000/questions Completed 302 Found in 14ms (ActiveRecord: 0.0ms) Started GET "/questions" for 127.0.0.1 at 2012-06-12 09:21:25 -0400 Processing by QuestionsController#index as HTML User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 585460615 LIMIT 1 Question Load (1.3ms) SELECT `questions`.* FROM `questions` WHERE `questions`.`user_id` = 585460615 Rendered shared/_error_messages.html.erb (0.0ms) Rendered questions/_form.html.erb (23.9ms) (0.5ms) SELECT COUNT(*) FROM `questions` Rendered questions/index.html.erb within layouts/application (48.8ms) Question Load (1.6ms) SELECT `questions`.* FROM `questions` (0.4ms) SELECT COUNT(*) FROM `questions` WHERE `questions`.`user_id` = 585460615 Rendered /Users/gregorygrillone/.rvm/gems/ruby-1.9.3-p194/bundler/gems/gauges-58ad28a906b2/app/views/gauges/_gauge.html.erb (0.1ms) CACHE (0.0ms) SELECT `questions`.* FROM `questions` CACHE (0.0ms) SELECT COUNT(*) FROM `questions` WHERE `questions`.`user_id` = 585460615 Completed 200 OK in 72ms (Views: 62.2ms | ActiveRecord: 4.2ms) Started GET "/assets/application.css" for 127.0.0.1 at 2012-06-12 09:21:25 -0400 Served asset /application.css - 304 Not Modified (0ms) [2012-06-12 09:21:25] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true Started GET "/assets/application.js" for 127.0.0.1 at 2012-06-12 09:21:25 -0400 Served asset /application.js - 304 Not Modified (0ms) [2012-06-12 09:21:25] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true </code></pre> <p><strong>Question controller</strong></p> <pre><code>class QuestionsController &lt; ApplicationController respond_to :html, :json def index @question = current_user.questions.new @questions = current_user.questions.all end def create @question = current_user.questions.new(params[:question]) if !params[:update_button] if @question.valid? if params[:next_button] || !@question.save @questions = current_user.questions.all render 'index' elsif !params[:next_button] &amp;&amp; params[:submit_button] &amp;&amp; @question.save flash[:success] = "Your question and answer have been saved." respond_with @question, :location =&gt; questions_path end else @questions = current_user.questions.all render 'index' end else @questions = current_user.questions.all render 'index' end end def next @question = current_user.unanswered.first @answer = Answer.new(:question =&gt; @question, :user =&gt; current_user) respond_to do |format| format.js end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload