我的评论已成功保存到我的表单remote: true
和我的搜索中,以便更新这些新评论而无需重新加载整个页面我需要使用create.js.erb文件以呈现注释部分。这都是在bootstrap模式中进行的。
create.js.erb
$(".instacomments").html("<%= escape_javascript(render partial: "comments/comment", collection: photo.comments, as: :comment) %>");
CommentsController
def create
@photo = Photo.find(params[:photo_id])
@comment = @photo.comments.build(comment_params)
@comment.save
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
评论/ _comment.html.erb
<%= link_to comment.user.name, user_path(comment.user_id)%>
<%= comment.content %>
UsersController
def show
@user = User.find(params[:id])
@photos = @user.photos.order('created_at desc').paginate(page: params[:page], per_page: 12)
end
Users / show.html.erb
<% @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")%>
</a>
<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 %>
</div>
</div>
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'
答案 0 :(得分:2)
ActionView :: Template :: Error(未定义的局部变量或方法 `照片&#39;对于#&lt;#:0x007fa5594987d8&gt;)
您需要在photo
@photo
更改为create.js.erb
$(".instacomments").html("<%= escape_javascript(render partial: "comments/comment", collection: @photo.comments, as: :comment) %>");
答案 1 :(得分:-1)
您可以使用此语法,它将正常工作
@synthesize map11;