Cannot Convert Symbol to Integer + Rails 3.2 Nested Attributes

I am working on a simple project to test the nested attributes of Rails 3.2. However, I get this error when trying to submit a form:

can't convert Symbol into Integer

post.rb and comment.rb

class Post < ActiveRecord::Base
  attr_accessible :title, :comments_attributes
  has_many :comments

  accepts_nested_attributes_for :comments
  validates_presence_of :title
end

class Comment < ActiveRecord::Base
  attr_accessible :comment, :author

  belongs_to :post

  validates_presence_of :comment
  validates_presence_of :author
end

posts_controller.rb

def new
  @post = Post.new
  @post.comments.build

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @post }
  end
end

_form.html.erb

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>

  <%= f.fields_for :comments_attributes do |builder| %>
    <fieldset>
        <%= builder.label :comment %><br />
        <%= builder.text_field :comment %><br />

        <%= builder.label :author %><br />
        <%= builder.text_field :author %>
    </fieldset>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Parameters

{"utf8"=>"✓",
 "authenticity_token"=>"gNA0mZMIxkA+iIJjw8wsddcKxvmzaFnrgiHvFw1OrYA=",
 "post"=>{"title"=>"Dummy Title",
 "comments_attributes"=>{"comment"=>"Dummy Comment",
 "author"=>"Dummy Author"}},
 "commit"=>"Create Post"}
+3
source share
1 answer

I agree with the comments that this is difficult to eliminate without the stacktrace and create method, but this suggests that it looks weird:

 <%= f.fields_for :comments_attributes do |builder| %>

Fields for your objects comment, right? unlike the comment_attributespost object (the latter does not make sense here, at least in the first reading).

You can try changing :comments_attributesto :comments.

+4
source

All Articles