Timmy Von Heiss Timmy Von Heiss - 5 months ago 66x
Javascript Question

Correct syntax to render rails partial in create.js.erb

My comments are being successfully saved to the database from my form,

remote: true
and from my searching, in order for these new comments to be updated without reloading the entire page I need to use a create.js.erb file to render the comment partial. This is all taking place in a bootstrap modal.


$(".instacomments").html("<%= escape_javascript(render partial: "comments/comment", collection: photo.comments, as: :comment) %>");


def create
@photo = Photo.find(params[:photo_id])
@comment = @photo.comments.build(comment_params)
respond_to do |format|
format.html { redirect_to :back }


<%= link_to comment.user.name, user_path(comment.user_id)%>
<%= comment.content %>


def show
@user = User.find(params[:id])
@photos = @user.photos.order('created_at desc').paginate(page: params[:page], per_page: 12)


<% @photos.in_groups_of(3, false).each do |group| %>
<div class="row instagram">
<% group.each do |photo| %>
<a data-toggle="modal" href=<%="#"+"#{photo.id}"%>>
<%= image_tag(photo.picture.ad.url, class: "img-responsive")%>
<div class="modal" id=<%="#{photo.id}"%> tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
<div class="col-sm-4">
<div class="instacomments">
<%= render partial: "comments/comment", collection: photo.comments, as: :comment %>

Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"e2P+/N1vg98AhUbNVD7j29sRRSUPsmI5+sMmqWXF9dUPosLSSn9AtJPL8KVsZ+u2c6FHyYRC7kEdGs/dBBaKjw==", "comment"=>{"content"=>"trrt"}, "commit"=>"Create Comment", "photo_id"=>"23"}
[1m[36mUser Load (0.7ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 2]]
[1m[35mPhoto Load (0.9ms)[0m SELECT "photos".* FROM "photos" WHERE "photos"."id" = $1 LIMIT 1 [["id", 23]]
[1m[36m (0.4ms)[0m [1mBEGIN[0m
[1m[35mSQL (1.1ms)[0m INSERT INTO "comments" ("content", "user_id", "photo_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["content", "trrt"], ["user_id", 2], ["photo_id", 23], ["created_at", "2016-05-05 05:24:59.845110"], ["updated_at", "2016-05-05 05:24:59.845110"]]
[1m[35mArticle Load (1.2ms)[0m SELECT "articles".* FROM "articles" WHERE "articles"."approved" = 't' ORDER BY "articles"."created_at" DESC
Completed 500 Internal Server Error in 128ms (ActiveRecord: 16.5ms)

ActionView::Template::Error (undefined local variable or method `photo' for #<#<Class:0x007fa559499cc8>:0x007fa5594987d8>):
1: $(".instacomments").html("<%= escape_javascript(render partial: "comments/comment", collection: photo.comments, as: :comment) %>");
app/views/comments/create.js.erb:1:in `_app_views_comments_create_js_erb__795660071360205666_70173660624520'
app/controllers/comments_controller.rb:8:in `create'


ActionView::Template::Error (undefined local variable or method `photo' for #<#:0x007fa5594987d8>)

You need to change photo to @photo in create.js.erb

$(".instacomments").html("<%= escape_javascript(render partial: "comments/comment", collection: @photo.comments, as: :comment) %>");