Post on a different page in ruby on rails - ruby-on-rails

I have been trying to solve a problem for some time in ruby on rails, but I haven't been able to achieve it and I can't seem to find a solution online (it must be easy but I am not sure what is the write thing to search for)
So, in my web application, I have a CURD table and I use modal to create new items in there:
Image 1
Image 2
This is working perfectly fine. What I would like to do is that when this is created I'd like to post in a different page that " ABC have been created by X User"
In my case that would be the chatbox container:
Image 3
So in my case, the green box is where I would like to say of what has been created and who has created it. I know that it is not a complex problem but I just can't seem to find the solution and I have been trying this for days.
Would really appreciate your help. Please let me know if you don't understand the problem and I can elaborate.
Thanks in advance.
Kind Regards,
Usman

You can create a separate model Notification (or similar name), and use an after_create hook in your original model to automatically create an instance of Notification. The Notification could store the information you want to display as attributes. From there, you'd have to figure out how to display the notification in the correct place on the 3rd page. One approach would be to query all your posts and notifications and sort them by created_at. There's other ways, it's up to you.

I get what you're saying.. but I am a little confused on the first part. So I have a model called Solr that has all the records associated with its User. What do I put in the after_create hook method to display records according to the timestamp in my Chat Box/ Events Log? Do you have an example of a similar implementation?
PS - Why do you say that I need to create a new model why can't I use the same model?
Thanks.

Related

Modeling an inbox in Rails

I'm writing an app that allows users to feed their inbox, then process their input, making it either a note or a task.
Since I need notes and tasks to behave very differently, I plan on using different models for them.
The question is: how do I go about changing inbox items into notes or tasks?
My first idea was to write an action that would destroy an inbox item and call the create action on the notes (or tasks) controller, but that does not seem right.
Is there a better way to do this?
Update
I'm looking into polymorphic associations as a solution for this, as suggested by #Dipak.
This is my schema: (sorry I can't paste the code. I'm using a web based ssh tool)
I have decided to use the Idea model to define inbox items
And these are my models:
I want to be able to click this link (on my partial)
and have it do two things:
create a task
assign this task to idea.thought
How should I do this? Is this the proper way to use this kind of association?
As per your question it seems like your few initial fields are same for all notes and tasks, that you calling inbox. So better you can save those fields in different table and other fields in related table.
For such situation you should use polymorphic association. You can read about it here. So your new inbox table should be intermediate table used for polymorphic association.

Creating history or log for certain events

I am not sure what is better perfomance whise so I ask you guys.
The problem is following:
I have a system where each User gets a certain amount of credit for certain events. So I gave my User an attribute named creditscore that gets altered on those events. Everything works well. But now I want the user to actually see what he did when and how much credit he got for this.
What would be better here:
Saving the whole history in a text attribute and add lines for each event
or
writing an extra model associated with the user and create an instance for every event.
or
or
something way different?
Since there are several events per user per day it would be either a huge text or a huge amount of instances. What would be better looking at website performance.
You absolutely do NOT want to store the history in a text attribute. Management of this will be a nightmare as will querying the data.
You could create a CreditEvent model and store the individual events in there. That would work fine.
However, before you start, check rubygems.org and ruby-toolbox.com to see if someone has already done the hard work. I know of at least one gem that seems to do exactly what you want to do:
https://github.com/merit-gem/merit

How to Change/update grails view (gsp) from service class

I have a Grails Application, which has a self implemented chat system. Now I am trying to refresh the sit ( or the box containing the messages in particular), as soon as a new Message arrived. So far I figured out three methods:
Poll from DB every second (every incoming message is saved to DB), which would be the easiest, but create a lot of unneccessary DB usage
Update the view from within the Messagelistener. I dunno how to do this though, what I am looking for is kind of the remoteFunction-tag as a function to call from within a service.
Update the view from domain class via beforeInsert-event. This is my least favourite option, plus I don't know how to do it for the same reasons as option 2.
If someone has a better option or a way to realize one of mine I would be very thankful :)
try http://vertx.io/ out. It's easy to setup and should do just fine for asynch-messaging

How to make logo interchangeable, Ruby on Rails

I have created a custom CMS Rails app for a local company, and had the request to allow the logo to be interchangeable for the holidays. For example, they talked about having choices of different logos for each holiday.
I am imaging a radio button looking something like this
Normal
Christmas
Christmas1
Easter
Easter1
Thanksgiving
4th of July
etc
So, does anyone have any idea how I would implement this, or have any experience with it?
I have an admin panel for them and I am thinking of adding a section that has the radio buttons mentioned above, and depending on what one is set a variable changes values and displays a different logo from the images folder, but not sure if that's the route to go.
Thanks for any help.
Edit: Sounds like I have the right idea, can I get some advice from you experts on how you would go about implementing this? I'm thinking of having a logo model where they can upload the image to, but how would I implement that into the view to allow them to pick?
Don't confuse two different things you will store in the database: the list of logos, and the setting of the current logo. The former will consist of a model and a table. The latter could be a simple foreign key pointing to the correct entry in that table.
You should also have a new controller since you plan to let them manage the list of logos. For image uploading check out Carrierwave which has comprehensive examples.

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.

Resources