2014-02-21 42 views
0

無法批量分配受保護的屬性:答案。 (使用Rails3)無法將大量屬性分配給註釋?

我不知道爲什麼它不允許我這樣做,因爲我可以訪問我的嵌套屬性。

這就是我的回答模式

class Answer < ActiveRecord::Base 
    has_many :comments, dependent: :destroy 
    belongs_to :question 
    attr_accessible :anonymous, :answer, :commenter, :votes, :comments_attributes 
    accepts_nested_attributes_for :comments 
end 

這是我的意見模型

class Comment < ActiveRecord::Base 
    belongs_to :answer 
    attr_accessible :anonymous, :comment, :writer, :votes 
end 

我的觀點

<%= form_for([@answer, @comment]) do |f| %> 

    <p> 
     <%= f.label :comment %> 
     <%= f.text_area :comment, :cols => "50", :rows => "30"%> 
    </p> 
    <p> 
     <%= f.submit "Submit Comment" %> 
    </p> 
<% end %> 

未能在這種形式這是我在功能我的commentsController是顯然導致錯誤

def create 
    @answer = Answer.find(params[:answer_id]) 
    @comment = @answer.comments.new(params[:comment]) 
    @comment.save 
     redirect_to question_path(@answer) 
end 
+0

什麼版本的Rails,您使用的?看你的Gemfile。 – Mohamad

+0

使用導軌3 - – google1254

回答

0

您的觀看代碼在技術上並不正確。您需要使用fields_for:

<%= form_for([@answer, @comment]) do |f| %> 

    <p> 
     <%= f.fields_for :comments do |u| %> 
      <%= u.label :comment %> 
      <%= u.text_area :comment, :cols => "50", :rows => "30"%> 
     <% end %> 
    </p> 
    <p> 
     <%= f.submit "Submit Comment" %> 
    </p> 
<% end %> 

您可能還需要從form_For助手中刪除@comment。在fields_For中,您可能需要user:comment或者甚至@comment。

如果使用的是軌道4,那麼你也將有一個問題,具有很強的參數:http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

+0

這是爲什麼?而且,這似乎並不能解決問題 – google1254

相關問題