0
Rails 5.2

In my views/books/show.html.slim, I have:

.row
  = render 'books/menu_items'
.row
  .col-lg-6
    = render partial: 'books/index', :locals => {books: @books.author_id}
  .col-lg-6
    = render partial: 'books/comments', :locals => {book_id: @book.id}
(some additional code to draw the page)

In my views/books/_menu_items.html.slim, I have:

button.btn.btn-primary type="button" data-target="#BookComment" data-toggle="modal" data-html="true"
  = "Comment On Book"
span#CommentNotice
  - if !!flash[:notice]
    = flash[:notice]

when the page first loads, the two tables in the partials are populated.

On top of the page, when I click on the Comment On Book button, it activates the following method in the Books Controller:

In my controllers/books_controller.rb, I have:

def create_author_comment
  @action = dosomething
  respond_to do |format|
    if @action
      format.js {render :partial => 'books/comments_refresh', :format => :js}
      flash.now[:notice] = 'New Comment Recorded'
    end
end

And, in books/comments_refresh.js.erb, I have some code that refreshes the books/comments partial, without refreshing the entire page.

When I click on the button, the modal appears, I fill it, and submit, the modal disappears, and the comments table 'books/comments' refreshes instantly, without re-rendering the page, but the success notification does not appear.

Any ideas?

EastsideDev
  • 6,257
  • 9
  • 59
  • 116
  • You render the partial before you set the flash notice, switch them around and it should work – Mark Dec 14 '19 at 21:16
  • I tried it both ways, and it still does not work – EastsideDev Dec 14 '19 at 21:31
  • What does your partial look like? do you actually have some js code to show the flash message on that partial? – arieljuod Dec 14 '19 at 22:26
  • There is no JS code on the partial to show the flash message. It simply refreshes the comments table. I listed in my question where I have the flash alert is called (the controller) – EastsideDev Dec 14 '19 at 23:44
  • [go through this link](https://stackoverflow.com/a/22566171/6341938). it might be helpful. – Pradeep Kumar Dec 15 '19 at 07:34
  • @PradeepKumar This does not address the issue I am asking about. flash is a session variable, and I can set it to anything I want. The problem is, that it is not seen until the page refreshes. – EastsideDev Dec 16 '19 at 01:31

1 Answers1

0

You only refresh books/comments. But the code that displays the flash message is contained in books/menu_items. It needs to be processed again by the backend as well.

beauraF
  • 100
  • 8