2012-05-09 10 views
3

Rails 3.2のネストされた属性をテストするための簡単なプロジェクトを進めています。はSymbolを整数+ Rails 3.2に変換できませんネストされた属性

can't convert Symbol into Integer 

post.rbと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 %> 

パラメータ

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

エラーが発生した場所、つまりコードの行とファイルを教えてくれることはありませんが、私たちはお手伝いできません。完全なスタックトレースも役立ちます。 –

+0

Holgerちょうどいい、完全なスタックトレースとposts_controllerの作成メソッドが必要です。 –

答えて

4

私はそれがスタックトレースせずにトラブルシューティングやメソッドを作成するのは難しいのですが、それは言った、これは奇妙に見えるという意見に同意:

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

フィールドあなたのcommentオブジェクト用ですか?ポストオブジェクトのcomment_attributesとは対照的に(後者は少なくとも最初の読書では意味がない)

:comments_attributes:commentsに変更してみてください。

+0

Steveに感謝します。これは、ネストされたモデルの検証を含む現在動作しています。 :-) – Ben

+0

なぜ:comments_attributesからcommentsに変更しようとすると、そのtext_fieldはUIに表示されません。 – hainguyen