Update content when new values are added to database? - ruby-on-rails

I know my question might be ovbious for some of you, but I'm not sure how to do it, and I can't find it on google, probably because I'm not using the right keywords.
I'm using the latest rails. On my view I have a table generated by rails. People are able to add content to that table by sending a rails form.
I want this table to be updated with newly updated values, without having to refresh the page.
For example, if user 1 sent the rails form and added a new value to the database, I want user 2 to see this update without having to refresh the page.

I would highly suggest watching the Polling for Changes - Railscast that covers exactly what you are looking for.
This essentially will cover how to use jQuery to poll the server to see if there are any changes, and apply them to the page without refreshing(ajax).

Related

Update form based on dropdown selection

I'm new to Rails and I'm trying to figure out how to change either the fields in the form, or the form itself, based on the selection of a drop down menu. I have thought about using AJAX, or about embedded Ruby within the form. Any direction would be great.
Well, obviously there will be javascripts involved at some point.
I think there are 2 solutions for you to choose from:
using ajax to fetch the new data to change your form upon dropdown changes
loading everything you need from the first time (when landing to the page) and then displayhing/hiding things only using javascript
Starting from this, I suppose your decision will be made depending if you want/can afford loading everything you need from the beginning... and if not you'll probably go with ajax.
Let us know if you need some more details. : )

Displaying survey questions one at a time MVC mobile

I'm trying to develop page for displaying survey questions on a mobile view using mvc. I have to display one question at a time and on clicking next it should display next question.
Am having hard time to achieve this. I initially thought I could using paging, as all the questions are inside a List collection. but then I get struck on how to save the answers selected by user for each questions. From performance side its wise to send a collection back to business layer to save all the selected answers to DB rather than one at a time.
For desktop browser I displayed all the questions on single page and one submit button at the end of the page. So on there is no issue with this one. Problem arises with mobile view.
So could someone please suggest on how to achieve this.
Thanks in advance,
Sai
From performance side its wise to send a collection back to business layer to save all the selected answers to DB rather than one at a time. - i dont think it would be much of a performance issue.
still.there are multiple options for the work you wanna do.
Use cookies as the storage mechanism - this way answers can be stored as key values pairs key being the index of the question.
this way every time you request a new question using ajax via jquery or simpel post back you can you can set cookie to the answer you have got.
save it at server side using session storage same as key value pairs.
use HTML 5 storage.
you can use this to store data at client side using jquery
I would store the questions in a database. Then each user, if the site is accessed via a mobile device (could detect via JavaScript) you would then save a field in their info (in the database) containing the index of the question that they're up to. I think this is a good idea as it would work cross device, if they start on a phone, then move to a tablet, their 'session' will still persist. Then when they're up to the next question, change the index, and query the database using the user's question index for the relevant information to produce the question.

Rails: need to update loaded pages' content updated

I know Stackoverflow doesn't want discussions, so I will try to ask an answerable question here: basically, I am building a admin area with naught but a table that has a few columns like project name, due date, sort of normal stuff.
But is there a technique that allows non-polling updating of when attribute(s) changes in the server, it gets reflected on the user's loaded page?
The table's data comes from a JSON call to the server, and it gets rendered with some javascript onto the table. Real simple stuff. If you must ask for an example. sure, just a table of first and last names.
Homer | Simpson
Lisa | Simpson
Bart | Simpson
This page is opened on many of our users, then if I change Homer to Remoh, without having the user refresh the page, I want the updated name be, well, updated on the table display.
Does Websocket or the pub/sub pattern have something to do with this?
Thank you!
You're looking for a websocket or pub/sub system, exactly as you think.
If this is a Rails application and you're using AJAX stuff -- and it sounds like both things are true -- then your best bet is Juggernaut, which makes the entire process seamless and easy.
It's relatively painless to use, and the author has a great sample app called Holla that almost solves your problem by itself.
If I understand your question correctly, you want all changes to the model data to reflect on the admin panel without the need for refreshing the page. That sounds like a job for some simple. AJAX.
In your js.erb file for your admin page, poll for changes every x seconds and if the results of that query are different than whats currently being displayed. Update the table's data.
Of course this is limited to how often you are calling the function with setTimeOut, but the plus side is that you can tweak that to be just what you like.
If you'd like something more 'out of the box' and more instantaneous. I'd go with #Veraticus's suggestions.

Loading data from database by Ajax - Ruby on Rails app

Sometimes at websites all comments or other data from DB is hidden by default. When user click at link like "Display comments" all comments from database are dynamically selected and placed under the content. It must be great for mysql performance, because content is generated only when user excatly need it. I would like to implement this stuff at my app.
I've got one idea to do this so far. Remote action with #comments = Content.comments and next page.insert_html at RJS template. Is it good idea or maybe I should choose different way?
The decision is purely based on the application that you are developing. For example if in case of stack overflow it does not make sense to show only the question and show answer link. But in case of a blog post it may be fine.
In the above situation, I don't think there will be a good improvement in performance by removing the comments of the content on show page. We can achieve the same functionality by making use of javascript methods. Hide the content on page load and show in on client request.

How to grab a record ID from a search form in rails

I'm very verrry new to rails.
I'm using the autocomplete (plugin) text field to browse through titles of my records. When the user selects the record, I'd like to forward them to a custom built URL, based on that record's ID.
How should I do this?
Thanks!
-Elliot
To be more specific. I'm trying to make a simple search form, that redirects right to the record filled in by the autocomplete. If the record does not exist, I'd either like a message saying it doesn't yet exist, or a create record page.
UPDATE:
This may be more helpful, how can I just grab the value currently in the text box?
The auto-complete plugin you are using may not be the best for you to use. Here is another option for you to consider:
Model Auto Completer
This plugin returns the text, but also stores the id in a hidden field.
There was another plugin I thought that did this as well, I can't remember it now though.
I think what you are referring to is directing to a page like "/record/my_awesome_product" rather than the boring "/record/1234".
Although this article is a little outdated (in Rails development terms) it still may help you.

Resources