ruby on rails 4 - cannot save data using a nested form with the cocoon gem -
i having trouble saving data using nested form cocoon gem, , haven't been able find solution on so.
i have 2 models: requests has_many votes.
i create single form saves new request, , new vote simultaneously. issue neither new request nor new vote saved using below code. i've pasted terminal output below completeness.
terminal output:
started post "/requests" ::1 @ 2015-06-25 15:41:17 +0100 processing requestscontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"fchco1rlg2zr6mo8mpmvnmeasvpraqyhay5d2sdrldun83ppnsrhquu33yioz84c4z1xmyqi4gzjlymn1nl3fw==", "request"=>{"firstname"=>"sdf", "lastname"=>"sf", "email"=>"sdf@lskdjf.com", "request"=>"lskdj", "description"=>"sldkjf", "votes_attributes"=>{"1435243272926"=>{"comment"=>"sadflkj", "email"=>"lkfj@sldk.com", "beta"=>"1", "_destroy"=>""}}}, "commit"=>"save"} (0.2ms) begin transaction vote exists (0.2ms) select 1 one "votes" lower("votes"."email") = lower('lkfj@sldk.com') limit 1 request exists (0.1ms) select 1 one "requests" lower("requests"."email") = lower('sdf@lskdjf.com') limit 1 (0.1ms) rollback transaction rendered requests/_vote_fields.html.haml (0.9ms) rendered requests/_vote_fields.html.haml (1.0ms) rendered requests/_form.html.haml (8.4ms) rendered requests/new.html.erb within layouts/application (9.2ms) completed 200 ok in 118ms (views: 105.8ms | activerecord: 0.5ms)
models:
class request < activerecord::base has_many :votes, dependent: :destroy accepts_nested_attributes_for :votes end class vote < activerecord::base belongs_to :request end
requests controller
class requestscontroller < applicationcontroller before_action :set_request, only: [:show, :edit, :update, :destroy] def create @request = request.new(request_params) if @request.save redirect_to @request else render 'new' end end private def set_request @request = request.find(params[:id]) end def request_params params.require(:request).permit( :id, :firstname, :lastname, :email, :request, :description, :votes, :tag_list, votes_attributes: [:firstname, :lastname, :email, :comment, :beta, :id, :_destroy] ) end end
requests/_form.html
= form_for(@request) |f| .field= f.text_field :firstname, placeholder: "requester firstname" .field= f.text_field :lastname, placeholder: "requester lastname" .field= f.text_field :email, placeholder: "email" .field= f.text_field :request, placeholder: "request" .field= f.text_field :description, placeholder: "description" #votes = f.fields_for :votes |vote| = render 'votes/vote_fields', :f => vote .links = link_to_add_association 'add vote', f, :votes, :render_options => { :wrapper => 'inline' }, partial: 'votes/vote_fields' .action= f.submit "save"
requests/_votes_fields.html.haml
.nested-fields .form-inline = f.text_field :comment, :placeholder => 'comment' = f.text_field :email, :placeholder => 'email' = f.check_box :beta, :placeholder => 'beta', :as => :boolean .links = link_to_remove_association "remove vote", f
votes , model validations:
vote.rb belongs_to :request before_save { self.email = email.downcase } #to ensure email uniqueness validates :email, presence: true, length: { maximum: 250 }, uniqueness: { case_sensitive: false } validates :request_id, presence: :true request.rb belongs_to :user has_many :votes, dependent: :destroy accepts_nested_attributes_for :votes before_save { self.email = email.downcase } validates :request, presence: true validates :firstname, presence: true, length: { maximum: 50 } validates :lastname, presence: true, length: { maximum: 50 } validates :email, presence: true, length: { maximum: 250 }, uniqueness: { case_sensitive: false } acts_as_taggable
Comments
Post a Comment