Home>

Environment

Ruby 2.7.2
Rails 6.1
macos

premise

Implemented an asynchronous comment posting feature with reference to the following articles.

[Rails] Implementation of comment function asynchronously using AJAX

I want to solve

I want to implement the comment posting function asynchronously!

Try to try

Because there is a problem with Render,Render: indexInredirect_to root_pathTransitioned to the root page where you changed and changed.
Also, even with the deletion functionRender: indexUsing, deletion is working properly.

Error statement, tried the following reference articles, but the same error has been displayed.

To return a JS file in Render Response

How do you think about how to do it.
I would appreciate your advice if you would understand.
Thank you very much.

Occurrence Issues /Error

After posting a comment, the following error statement is displayed and it is not working properly.

Applicable source code
Travel_Records_Controller.rb

DEF SHOW
    @Travel_Record= TravelRecord.find (Params [: ID])
    @user= @ travel_record.user
    # Create comment
    @Comment= Comment.new
    # Show in new order order
    @Commments= @ travel_record.comments.order (CREATED_AT :: DESC)
  END

comments_controller.rb

Class CommentsController <
 ApplicationController
  DEF CREATE
    @Travel_Record= TravelRecord.find (params [: travel_record_id])
    # Create a comment linked to a post
    @Comment= @ travel_record.comments.build (Comment_Params)
    @ Comment.User_ID= CURRENT_USER.ID
    @ Comment.Save
    Render: index
  END
  DEF DESTROY
    @Comment= Comment.Find (Params [: ID])
    @ Comment.Destroy
    Render: index
  END
  premium
    Def Comment_Params
      params.require (: Comment) .Permit (: comment,: user_id,: travel_record_id)
    END
END

index.js.erb

$ ("# comments_area"). HTML ("<
%= j (render 'index', {comments: @ Comment.travel_record.comments})% >
");
$ ("TextArea"). Val ('');

TRAVEL_RECORDS /SHOW.HTML.SLIM

= Render 'Layouts /Flash_Messages'
#Onomments_area
= render 'comments /index', comments: @ comments

comments /_index.html.slim

= Comments.count
Comment of
H6.more [Data-Toggle= "Collapse" Data-Target= "# collapseExample" ARIA-EXPANDED= "FALSE" ARIA-CONTROLS= "CollapseExample"]
  More ... more ...
-COMMENTS.FIRST (2) .EACH DO | COMMENT |
  -Unless Comment.id.nil?
    P= LINK_TO "@ # {Comment.User.Name}", user_path (comment.user.id)
    p
      | Comment:
      COMMENT.COMMENT
      -If Comment.User== Current_User
  = LINK_TO TRAVEL_RECORD_COMMENT_PATH (Comment.Travel_Record_ID, Comment.id), Method: DELETE, REMOTE: TRUE DO
          i.fas.fa-trash [style= "color: black;"]
    span= Comment.created_at.Strftime ('% y /% m /% d% h:% m:% s')
# Collapseexample.collapse
  -COMMENTS.OFFSET (2) .EACH DO | COMMENT |
    -Unless Comment.id.nil?
      P= LINK_TO "@ # {Comment.User.Name}", user_path (comment.user.id)
      p
        | Comment:
        COMMENT.COMMENT
        -If Comment.User== Current_User
    = LINK_TO TRAVEL_RECORD_COMMENT_PATH (Comment.Travel_Record_ID, Comment.id), Method: DELETE, REMOTE: TRUE DO
            i.fas.fa-trash [style= "color: black;"]
      span= Comment.created_at.Strftime ('% y /% m /% d% h:% m:% s')

comments /_Form.html.slim

= form_with model: [Travel_Record, Comment],
 URL: Travel_Record_comments_Path (Travel_Record) Do | F |
= f.text_area: Comment
= f. Submit "Comment", class: 'BTN BTN-Primary BTN-Block'