javascript - Why is my partial not rails rendering properly? -
i want add comments without having refresh page in app. however, partial no load js functionality implementing.
i getting no errors in log or console, , comment shows once refresh page, not happen without refresh.
also, when write js append last comment win_com id, code works. i'd html dynamically show current state of comment partial via js rather append last comment. appreciated! here's code:
comments_controller.rb:
def create @window = window.find(params[:window_id]) @comment = @window.comments.build(comment_params) @comment.user_id = current_user.id if @comment.save respond_to |format| format.html { redirect_to post_path(@post) } format.js end end end views/windows/show.html.erb: (part of larger view)
<div class="row col-md-7" id="win_com"> <h4>user comments</h4> <%= render partial: '/windows/comment', collection: @comments %> </div> views/windows/_comment.html.erb:
<div class="container"> <div class="row col-md-7"> <p><%= comment.body %></p> <p><%= link_to "delete", [comment.window, comment], method: :delete, data: {confirm: "are sure?"} %></p> </div> </div> views/comments/create.js.erb:
$('#win_com').html("<%= j render(partial:'/windows/comment', collection: @comments)%>"); application.js:
//= require jquery //= require jquery_ujs //= require bootstrap-sprockets //= require turbolinks //= require_tree . maybe key lines sever log?:
rendered windows/_comment.html.erb (0.0ms) rendered comments/create.js.erb (1.8ms)
you missing @comments instance variable in controller action create. rails cannot magically know unless specify it. change create action this.
def create @window = window.find(params[:window_id]) @comment = @window.comments.build(comment_params) @comment.user_id = current_user.id if @comment.save @comments = @post.comments //given defined. respond_to |format| format.html { redirect_to post_path(@post) } format.js end end end
Comments
Post a Comment