0

我目前有一个应用程序,其中包含用户状态并支持对这些雕像的评论。一切正常,直到我决定在每个状态下方呈现一个评论表单。成功提交后,我的 create.js.erb 会将新创建的状态添加到用户的提要中。这是我的代码:

此代码呈现各个状态,位于 shared/feed_item

<li id="<%= feed_item.id %>" class="block">
  <%= feed_item.content %>
  <%= render 'shared/feed_comment_form', object: feed_item %> //works fine without this line
</li>

我的 create.js.erb 状态

<% if @status.errors.any? %>
    $("#new_status").replaceWith("<%= escape_javascript(render('shared/status_form')) %>");
<% else %>
    $("<%= escape_javascript(render('shared/feed_item', feed_item: @status)) %>").hide().prependTo('.feed').fadeIn(1000);
<% end %>

共享/feed_comment_form

<%= form_for([object, @comment], :remote => true) do |f| %>
  <%= render 'shared/error_messages_short', object: @comment %>
  <div class="field">
    <%= f.text_area :content, placeholder: "Enter comment...", rows: 1 %>
  </div>
  <%= f.submit "Post", class: "status-btn", id: "status_submit_#{object.id}" %>
<% end %>

这是错误

Rendered statuses/create.js.erb (9.4ms)
Completed 500 Internal Server Error in 21ms

ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
    1: <%= form_for([object, @comment], :remote => true) do |f| %>
    2:   <%= render 'shared/error_messages_short', object: @comment %>
    3:   <div class="field">
    4:     <%= f.text_area :content, placeholder: "Enter comment...", rows: 1 %>
  app/views/shared/_feed_comment_form.html.erb:1:in `_app_views_shared__feed_comment_form_html_erb__4480113129845938771_70094779148500'
  app/views/shared/_feed_item.html.erb:24:in `_app_views_shared__feed_item_html_erb___2728444254186520065_70094807848760'
  app/views/statuses/create.js.erb:4:in `_app_views_statuses_create_js_erb__3125686809617511276_70094769988780'
  app/controllers/statuses_controller.rb:6:in `create'

我不明白为什么变量 'object' 为空,因为我在 shared/feed_item 中传递了它 'feed_item'

任何帮助,将不胜感激。

4

1 回答 1

0

原来@comment 实际上是 nil 而不是“对象”。我从控制器传入@comment,我猜它没有通过ajax 传递给渲染。我不得不将@comment 更改为 Comment.new。不确定这是否是最好的解决方案

于 2012-09-24T03:19:13.260 回答