How to update or refresh table using Rails Actioncable? - javascript

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.

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.
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.

Related

How to add id or class to a div after updating the database in ruby-on-rails?

When I update a table, I see I can send a notice saying the table was updated.
What I am thinking about accomplishing is to have the row that was updated to blink a couple of times after I update that specific row.
So for example, I have a controller accounts with a name and email.
On update, I redirect to the accounts_url with the notice #account.name was updated.
How can I tell the page which of those rows was updated so I can have the row blink a couple of times?
I am just trying to have fun and learn, so don't be too harsh on the comments.
You can pass the updated record id like this to the accounts page:
redirect_to accounts_url(updated_account_id: #account.id)
and in the next page retrieve it from params[:updated_account_id] and do whatever you want with it.

url changed after last form submission how to go back on next selection?

I've got a taxonomy filter select form on /books/ of my site. When a user selects a filter they'll be at:
/books/?genre=novel
When the list is paginated and they go to page 2 the parameter will carry over and they'll be at: books/page/2/?genre=novel Which works fine.
But if now they pick a different option from the form and the new genre doesn't have 2 pages they will get a 404 page. Because /books/page/2/?genre=thriller doesn't exist, but /books/?genre=novel does.
How can i stop this from happening?
Just remove the paging in the genre change handler.
When the user changes the genre remove paging.
Set action to the html form:
<form action="/books/" <!--other parameters-->">
Solution found on this question.

How to reload JSP with changed actionbean variable using Stripes

I am new to working with Stripes. I have a dropdown ("show ## recordrs per page") and a paginated table on the JSP page and I want to display as many records per page in this table, as the value selected in the dropdown.
The action bean has a variable "recordsPerPage" and I am not able to figure out a way to set the value of this variable and reload the table, so as to change the number of records that are displayed per page. Please help.
---- Additional Info -----
The table that I use is a displaytag table, which accepts a PaginatedList. This table is within a stripes form.
-- EDIT:
What I did is, I added a <stripes:hidden/> with the name "recordsPerPage" and set the value to be the number of records I want to display. I also added a <stripes:submit> to the same form. The "name" attribute of this submit button is the method name of the action bean I have to call. When I click on this button, I am able to do what I want. But now, I am unable to do it through javascript. Please help.
Due to some reason, calling Dom.get('submitButton').submit() or Dom.get('myFormId').submit() does not fulfill the intended purpose. Taking a clue from being able to do what I wanted from the submit button click, I made this submit button hidden and the called click() on this button from javascript. Thus Dom.get('submitButton').click() produces the intended result. If someone can give me the explanation of why the submit method did not result in expected behavior, that'll be great.

Get elements of a query-set with a jquery get function. Django

In my django app I would like to display a text when a user clicks on a button, without refreshing the page. When the user clicks again, I would like an other text to be displayed, still without refreshing the page.
The informations (texts) I wan't to display are in a query set, named "information".
So I would like to know how to accomplish that.
Here is the way I try to do it:
I create a view where I store my query set:
def get_information (request):
information= Information.objects.filter(object1__id= X)
Then I create a jquery get function (in the template of the page where the user clicks) in order to get informations on this list:
function get_info(){
$.get('/mysite/get_information', {'information':information}, function(data) {
$('.information').html(data);
});
};
And then I render it in my template with a submit button and a onclick="get_info".
But I don't know how to make the request get a different information at each request, in order that the user does not get the same information twice.
Thank you a lot for your help.
Take a look in to Django Pagination. You could create the Paginator object in your view and then you can display message from the queryset based on the request from the client side. Let me know in case of any issues.

Rails nested form, create and then update new associated models without page reload

Relevant info
I have a User model with plenty of attributes and several has_many associations.
I have a BIG form to edit existing user info, including nested fields.
I have divided the form into several forms in different divs and show only one form at a time. (i.e. tabs in UI)
User can save info by clicking Save button, which submits currently visible part of form via Ajax. No page reload. Just updating a status box with rjs.
I use Ryan Bates example to dynamically add fields for nested models.
Problem
If a user adds fields for associated models using js
and submits form n times
n new associated models are created
this happens because js-added fields have no hidden_field for id attribute and every time form is summited, parameters do not include id for that specific nested record. i.e. first time it is ok, after second ajax post request (reminder - page does not reload) that record should be updated, but instead new record is being created because there were no id parameter passed for this already existing record.
Question
Should I make dirty hacks in controller to find out if new records should be saved and if they are - update dynamically added form in view by adding hidden field with newly saved record id value. The question is - how do I do it gently, writing the most maintainable and robust code possible?
At the moment update action is dead simple, with ideas of dirty hacks commented
def update
#user = current_user
respond_to do |wants|
wants.js {
#analyze parameters to find out which
#nested model is going to be saved (not updated)?
#also save identifier of nested record in view i.e. {...."324324234" => {:a => 1, :b => 2, .... etc}}
#user.update_attributes(params[:user])
# check if the record of the model was saved and take it's id;
# use identifier 324324234 to find nested form and insert hidden
# field with record id using rjs
}
end
end
I hope I have defined my problem clearly. I would really like to avoid such hacks and write pretty code. I also wouldn't like to reload the page or part of it with ajax. Any suggestions?

Categories

Resources