我有以下内容:
路线.rb:
resources :posts do
resources :replies
end
回复_controller.rb:
class RepliesController < ApplicationController
def create
@post = Post.find(params[:post_id])
@reply = @post.replies.build(params[:reply])
@reply.user_id = current_user.id
if @reply.save
flash[:success] = "reply created!"
redirect_to post_path(@post)
else
redirect_to post_path(@post)
end
end
回复/_form.html.erb:
<%= form_for([@post, @post.replies.build]) do |f| %>
<%= render 'shared/error_messages', object: f.object, target: @reply %>
<div class="field">
<%= f.text_area :content, placeholder: "Enter reply content" %>
</div>
<%= f.submit "Reply", class: "btn btn-large btn-primary" %>
<% end %>
帖子/show.html.erb:
<div class="span8">
<%= render 'replies/form' %>
</div>
共享/error_messages.html.erb:
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
我不确定为什么replies
我使用的错误消息没有显示target: @reply
(:content 和 :user_id 是必需的)。
有什么建议可以解决这个问题吗?