jquery - 在 create.js.erb 中渲染 Rails 部分的正确语法

标签 jquery ruby-on-rails

我的评论已从我的表单 remote: true 和我的搜索成功保存到数据库中,为了在不重新加载整个页面的情况下更新这些新评论,我需要使用create.js.erb 文件来呈现部分评论。这一切都在引导模式中发生。

创建.js.erb

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

评论 Controller

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 %>

用户 Controller

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'

最佳答案

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

您需要在create.js.erb中将photo更改为@photo

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

关于jquery - 在 create.js.erb 中渲染 Rails 部分的正确语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37043068/

相关文章:

javascript - 在后端 ASP.NET MVC (MEF) 上发布/绑定(bind)多个表单

jquery - DataTables 仅在页面刷新时加载

javascript - 使用最后一个文本框值填充新添加的文本框

ruby-on-rails - DatabaseCleaner + RSpec : what is the correct configuration?

ruby-on-rails - Rails - 上周按天分组

javascript - 异步提交表单以释放 UI

javascript - 通过 jQuery 使用 Spring REST API

ruby-on-rails - favicon.ico 被呈现为查询参数的一部分;可能的路线问题

ruby-on-rails - 使用 self 作为 ruby​​ 实例方法中的第一个参数

css - save_and_open_page 已停止提供我的 CSS