0

My action cable is working fine, it shows me an alert with the data content(although it shows in all the pages).

This is my case: an user with role ADMIN has access to an url (http://localhost:3000/ventas/) with a table (html id=tablaventas)that show a list of sales. This table must be automatically refreshed every time a user (role SELLER) inserts a new sale in the database.

This user with role seller has an url (http://localhost:3000/ventas/new) with a form where insert new sales to the database. The Actioncable implementation is working okay, but I have two problems:

1- how to refresh the table from the ventas.coffee:

//here i am stuck
 received: (data) ->
   alert data.name  //this works ok
   //...AND HERE I WANT TO UPDATE or refresh THE ADMIN TABLE

2- Also, how can I show for example the alert message only to the admin specific view? in this case only in the view ventas/index.html.erb.

halfer
  • 19,824
  • 17
  • 99
  • 186
matQ
  • 597
  • 2
  • 13
  • 27

1 Answers1

0
  1. Can you just add another row to the table?

$('#tablaventas').append("<tr><td>#{data.name}</td>...other tds...</tr>")

If you need to refresh the whole table - you'll need to call .html(data.table_html) on table container element. Also this means that you'll have to render table_html in your controller, via render_to_string('table') or something.

  1. I think your issue is that you subscribe to action cable updates globally while you only need to subscribe on the index page. This blog post should help you solve this.

Hope the above helps.

Yaro
  • 570
  • 3
  • 20