Reloading table, on Rails controller action
Reloading table, on Rails controller action
Rails 5.2
In my views/books/show.html.slim, I have the following:
= render partial: 'authors/index', locals: {author_id: @author_id, book_id: @book_id}
In my views/authors/_index.html.slim, I have:
table.table-striped#CommentsOnAuthors.display
javascript:
$('#CommentsOnAuthors').DataTable({
ajax: {
url: '/authors_comments',
data: {
author_id: "#{author_id}",
book_id: "#{book_id}"
}
},
columns: [
{title: 'Book', data: 'book'},
{title: 'Author', data: 'author'},
{title: 'Comment', data: 'comment'}
]
});
ِAnd in my controllers/authors_controller.rb, I have:
def index
comments = Comments.where(author: params[:author_id], book: params[:book_id])
render json: { data: activity_logs }
end
When the main page (views/books/show) load as the app starts, everything loads fine, and the table showing author comments is showing fine.
From another part of the main page, someone can enter a comment, and it goes into the comments table. The controllers/authors_controller.rb has a method to deal with that:
def create
comment = params[:comment]
author = params[:author]
book = params[:book]
@new_comment = Author.new(comment: comment, author: author, book: book)
end
If I refresh the main page, then the table refreshes and shows the new comment. However, what I want to do, is force a refresh, ONLY of the table, not the whole page, when a new comment is entered. Any ideas how to do this?
Answers
I have a typo above, but I can't find an edit button:
You could issue an ajax reload with
ajax.reload()
, that would get the new data back from the server.Colin
How would I do that?