投稿数の多いトピックを編集しようとしています。トピックの編集ページには、編集可能なトピックname
と投稿content
があります。
一括割り当てエラーは、topics_controller.rb、update
method、で発生しpost.update_attributes(params[:post])
ます。
一括割り当てエラーを回避するにはどうすればよいですか。
topic.rb
class Topic < ActiveRecord::Base
has_many :posts, :dependent => :destroy
belongs_to :forum
accepts_nested_attributes_for :posts, :allow_destroy => true
attr_accessible :name, :last_post_id, :posts_attributes
end
post.rb
class Post < ActiveRecord::Base
belongs_to :topic
attr_accessible :content
end
topics_controller.rb
def update
@topic = Topic.find(params[:id])
post = @topic.posts.first
if @topic.update_attributes(params[:topic]) && post.update_attributes(params[:post])
topic = Topic.find(@post.topic_id)
flash[:success] = "Success!"
redirect_to topic_posts_path(topic)
else
render 'edit'
end
end
views / topics / edit.html.erb
<%= form_for @topic do |f| %>
<!-- render 'shared/error_messages_topic' -->
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for @topic.posts.first do |post| %>
<%= render :partial => "posts/form", :locals => {:f => post} %>
<% end %>
<%= f.submit "Edit", class: "btn btn-large btn-primary" %>
<% end %>
ビュー/投稿/_form.html.erb
<%= f.label :content %>
<%= f.text_area :content %>