I currently have an application that contains user statuses and support for comments on these statues. Everything was working fine until I decided to render a comment form below each status. My create.js.erb prepends the newly created status to the user’s feed after successful submit. Here is my code:
This code renders individual statuses and is located in 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>
My create.js.erb for statuses
<% 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 %>
shared/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 %>
This is the error
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'
I don’t see why the variable ‘object’ is null because I am passing it ‘feed_item’ in shared/feed_item
Any help would be appreciated.
Turns out @comment was actually nil and not ‘object’. I was passing in @comment from the controller and I guess it’s not passed to a render through ajax. I had to change @comment to Comment.new. Not sure if this is the best solution