2016-05-05 40 views
1

我的註釋已成功從我的表單remote: true保存到數據庫,並從我的搜索中保存,以便在不重新加載需要的整個頁面的情況下更新這些新註釋使用create.js.erb文件呈現部分註釋。這一切都以自舉模式進行。在create.js.erb中渲染rails部分的正確語法

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 

用戶/ 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

您的文件名實際上是'create.js.erb'而不是'_create.js.erb'。請在問題中改變它以避免混淆。 – Pavan

回答

1

::的ActionView模板::錯誤(未定義局部變量或方法 `照片」爲#<#:0x007fa5594987d8>)

您需要create.js.erb

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

你可以改變photo@photo用戶這種語法它會正常工作

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

這沒有奏效。它需要是@photo而不是照片。謝謝。 –