I need to set a temporary variable (actually more of a "true" or "false") for a view.
The use case is that when a user is created, they are redirected to a dashboard page. For Google AdWords conversion tracking, there is a bit of code that needs to be displayed in the view, but it only should be displayed after the create method has been run.
So I'm guessing the way to solve that is to set some temp variable (#show_conversion or something) and set it for one view (similar to a flash message).
So, how do I do that?
<% unless #user.new_record? %>
<!-- do stuff in here -->
<% end %>
Related
Is it possible to create a QR code that redirects the user to a modal in a specific page?
As in, you send him to the page X but already trigger click <%= link_to "modaltoshow %> , and the user sees the modal immediately after scanning.
Thanks
If your question is simply "can I launch a page with a modal already open," the answer is yes. You'd just need a javascript that launches it on load, like the selected answer here.
Assuming you need some ruby logic, you could simply change the target of that script with embedded ruby based upon a parameter the QR code passes (for instance, if you have a parameter "target_modal" that is the target modal's id, just reference that in the script). It could look something like this in your script block:
<!-- The if statement just checks for the parameter. If the parameter will
always be present, you may remove this. -->
<% if params(:target_modal) %>
$(window).load(function(){
$('<%= params(:target_modal) %>').modal('show');
});
<% end %>
I have a navigation bar (in fact two) and I use a before action in some controllers to fill it's dynamic data (the second bar may not exist in some), I've seen a lot of complains about not passing a lot of instance variables to views, and all of them suggested passing locals in render. I've been wondering using a instance variable to generate these stuff in the main layout is a good idea or not, and if it's not, how should I do this, render seems to overwrite the default view and I use the data in the main layout only
I not sure that I understand well your question. But for some of my menus I use something like that in my layouts:
<%= yield(:menu_top) %>
and I use
content_for :menu_top
to generate content in this area.
For exemple:
<%= content_for :menu_top do %>
<li>my specific content or var</li>
<% end %>
Here is the rails guide for content_for: link
I am conducting partial caching which is working really well.
However if I change the DOM inside the cache block those changes arn't cached. Is there a way to also include those changes?
Here is what I have so far:
<%
cache(#contact.hash_key) do
%><div id="<%=#contact.hash_key%>"></div><%
end
%>
<script>
//use ajax to prepend new messages.
$(document).ready(function(){
$.get("/messages?cid=<%=#contact.hash_key%>", function(data) {
$("#<%=#contact.hash_key%>").prepend(data);
});
});
</script>
UPDATE
Ok so I am trying to attempt to cache the result before it enters the DOM with the same cache key. That way when the cache block is rendered the new data is included with that key.
But I'm not sure the correct way to structure this.
Done!
So for anyone wanting to do the same thing here is a brief outline on how to do it. You may need to make changes or tweak this solution to fit your own.
This will only work if you are using cache keys. In my instance I have 2 types. A contact.hash_key and a message.hash_key
All message caches are kept inside a parent contact cache. So essentially:
<div id='contact-hash'>
<div id='message-hash-1'>
<div id='message-hash-2'>
<div id='message-hash-3'>
First of all we need to loop through the messages and see if they are cached. If they are then you can just render the cached copy. You can do this by using the Rails.cache.read method:
messages.each do |message|
cache = Rails.cache.read 'views/'+message.hash_key
if cache.nil? == false
%><%= cache.html_safe %><%
end
end
So now we have a list of messages from cache thats already been loaded. So what about new messages? Lets load these via ajax so the user isn't waiting for a boring page load.
You will notice in my question above I have querying "/messages?cid=<%=#contact.hash_key%>" in my AJAX call. This is calling the messages controller and rendering the index view.
Before we want to load the rendered view into the DOM we want to write it to the cache first with, surprise surprise, the identical message.hash_key we are going to use to read it.
So in your view:
<%
cache(message[:hash_key]) do
%>
<div class='message'>
This is a new message from the server.
</div>
<%
end
%>
If in some situations this doesn't work (god knows theres so many app permutations out there) you can also use Rails.cache.write 'foo', 'bar' in the controller instead of caching it at the view level.
And there you have it. Now you can add the new hash_key to the list and then render the view back to the DOM as the results of your AJAX call.
Now with the new hash_key in the list you can loop over it and it will come up as a cached copy.
This may or may not be the most elegant solution. If someone wants to simplify or give any advice on it so I can improve it that would be much appreciated.
I have a model but one of its property is not displaying its value.
<% #medical_history.neonatalcourse %>
I already check my sqllite file and the value is there. So I am sure that I am saving the field. The problem is it not displayed.
Thanks
you need to use an equal sign with your tag to tell it to print the output of the ruby statement into the view. The version without the "=" is for when you don't wish to have a statement print its value into the page.
<%= #medical_history.neonatalcourse %>
I'm wondering if this is a good idea or not, either way I'd like you guys opinion on how to best achieve this sort of messaging popup system I have in my app.
Under some occasions I want to popup a lightbox for users when certain actions are triggered. I already have the lightbox working and opening when when my controller returns JS for a request.
Here is the senario, I want to check if a user has new messages when a new request is made, and if they do I want to show the messages in my lightbox when the new page is loaded.
Should I just put some JS at the bottom of my <body> and render it if the user has messages? Or should I use like flash[:notice] and have it render as JS or something... I'm a bit stuck you see.
Don't use flash notices, this is not what they are for at all. I would have something in the layout like this:
<% if (messages = current_user.new_messages).size > 0 %>
<%= javascript_tag "display_messages(#{messages.collect(&:message_text).inspect})" %>
<% end %>
obviously here i'm guessing at your messages' methods but you get the idea. .inspect will give it an array of strings, you could give it the message data as a json object or whatever.