Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 18th, 2012  |  syntax: None  |  size: 0.75 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. accepts_nested_attributes_for alias?
  2. class Topic < ActiveRecord::Base
  3.   has_many :choices, :dependent => :destroy
  4.   accepts_nested_attributes_for :choices
  5.   attr_accessible :title, :choices
  6. end
  7.        
  8. ActiveRecord::AssociationTypeMismatch (Choice(#70365943501680) expected,
  9. got ActiveSupport::HashWithIndifferentAccess(#70365951899600)):
  10.        
  11. def create
  12.     choices = params[:topic].delete(:choices)
  13.     @topic = Topic.new(params[:topic])
  14.     if choices
  15.       choices.each do |choice|
  16.         @topic.choices.build(choice)
  17.       end
  18.     end
  19.     if @topic.save
  20.       render json: @topic, status: :created, location: @topic
  21.     else
  22.       render json: @topic.errors, status: :unprocessable_entity
  23.     end
  24.   end
  25.        
  26. def choices=(params)
  27.   self.choices_attributes = params
  28. end