У меня есть вложенная форма, которая состоит из сообщения и ответов:
posts_controller.rb:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def show
end
def new
@post = Post.new
@post.replies.build
end
.
.
.
private
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:content, :user, replies_attributes: [:content])
end
end
replies_controller.rb:
class RepliesController < ApplicationController
before_action :set_reply, only: [:show, :edit, :update, :destroy]
def new
@reply = Reply.new
end
def edit
end
def create
@reply = Reply.new(reply_params)
respond_to do |format|
if @reply.save
format.html { redirect_to @reply, notice: 'Reply was successfully created.' }
format.json { render action: 'show', status: :created, location: @reply }
else
format.html { render action: 'new' }
format.json { render json: @reply.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @reply.update(reply_params)
format.html { redirect_to @reply, notice: 'Reply was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @reply.errors, status: :unprocessable_entity }
end
end
end
private
def set_reply
@reply = Reply.find(params[:id])
end
def reply_params
params.require(:reply).permit(:content)
end
end
post.rb:
class Post < ActiveRecord::Base
has_many :replies, :dependent => :destroy
accepts_nested_attributes_for :replies, :allow_destroy => true
end
reply.rb:
class Reply < ActiveRecord::Base
belongs_to :post
end
posts / _reply_fields.html.erb:
<p>
<%= f.label :content, "Reply" %><br />
<%= f.text_area :content, :rows => 3 %><br />
</p>
posts / _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 :content %><br>
<%= f.text_field :content %>
</div>
<div class="field">
<%= f.label :user %><br>
<%= f.text_field :user %>
</div>
<div class="replies">
<%= f.fields_for :replies do |builder| %>
<%= render 'reply_fields', :f => builder %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
schema.rb:
create_table "posts", force: true do |t|
t.string "content"
t.string "user"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "replies", force: true do |t|
t.string "content"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "post_id"
end
Вывод в частях ответов просто:
<div class="replies">
</div>
Теперь все работает нормально, если я отправлю форму: создается новое сообщение и ответ. Но каждый раз, когда я редактирую сообщение, создается новый ответ.
Итак, если я отредактирую такой пост:
post title: Post Title 1
post content: Post Content 1
reply content: Reply Content 1
Я получаю что-то вроде этого:
post title: Post Title 1
post content: Post Content 1
reply content: Reply Content 1
reply content: Reply Content 1
и так далее, и так далее ...
Я хочу может быть проблема?