Hi, I am trying to create a poll, which has many choices. While creating the poll, I am getting the "Poll can't be Blank" Error. Poll gets created when I removed the choices fields from the form
Poll.rb
class Poll < ActiveRecord::Base
belongs_to :profile
has_many :choices, :dependent => :destroy
accepts_nested_attributes_for :choices, :reject_if => lambda {|a| a[:content].blank?}
validates_presence_of :question, :profile_id
validates_numericality_of :profile_id
validates_length_of :question, :in => 5..400
end
Choice.rb
class Choice < ActiveRecord::Base
belongs_to :poll
validates_presence_of :poll_id, :content
validates_numericality_of :poll_id
validates_length_of :content, :maximum => 300
end
PollsController
class PollsController < ApplicationController
def new
@poll = Poll.new
4.times { @poll.choices.build}
end
def create
@poll = Poll.new(params[:poll])
@poll.profile_id = session[:user_id]
if @poll.save
flash[:notice] = "Poll is created"
redirect_to poll_path(@poll)
else
render :controller => :polls, :action => 'new'
end
end
end
polls/new.html.erb
<% form_for (@poll) do |f|%>
<%=f.label :question%>
<p>
<%=f.text_area :question, :rows => 5, :cols => 40%>
<%=f.hidden_field :profile_id, :value => session[:user_id]%>
</p>
<% f.fields_for :choices do |builder|%>
<p>
<%=builder.label :content, "Choice"%>
<%=builder.text_field :content%>
</p>
<%end%>
<p>
<%=f.submit "Create Poll"%>
</p>
<%end%>
Can somebody help me fix this problem. Thanks!!!