我一直在阅读 michael hartl 的 ruby on rails 教程,我正在尝试发布微博。我正在尝试在表单中呈现错误消息,并且书中说将 f.object 分配给对象。它不断给我“对象”是未定义的局部变量或方法的错误。这是代码:
共享/_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 %>
共享/_post_form.html.erb
<%= form_for(@post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new post..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>