How to Hide Foundation flash message? - ruby-on-rails

I'm really confused as to how to hide a flash message in my rails app. I've read the documentation but I'm not sure how to do this correctly. I was hoping someone could point me in the right direction.
For my app, I have a flash message/alert for submitting videos/comments/sign_in,sign_up etc. etc. Below is the flash code in my application.html.erb file:
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div data-alert class="alert-box <%= name.to_s == 'notice' ? 'success' : 'alert'
n%>">
<%= content_tag :div, msg %>
</div>
<% end %>
<% end %>
The part that I get confused is the jquery part. On Foundation, the docs say that to customize it you need these three files:
<script src="js/vendor/jquery.js"></script>
<script src="js/foundation/foundation.js"></script>
<script src="js/foundation/foundation.alert.js"></script>
I thought these files were already in foundation. Do I need to create them under my app/assets/javascripts/ directory? Also, why would I need to add these at the bottom of my application file? I thought this tag
<%= javascript_include_tag "application" %> does it for me? It should because of the * require tree right?
I'm just confused about what goes where and what needs to go in what? For example, to hide the flash messages, where would I put this?
$(document).ready(function(){
$('.alert-box alert').fadeOut(1000);
$('.alert-box success').fadeOut(1000);
});
Does this go in the foundation.alert.js file,the foundation.js file, or the vendor/jquery.js file or somewhere completely different. Some help would be nice.So confused.......

I needed similar functionality once in which I had to fade out the flash message only on one specific page.
I created a separate JS file and required it in application.js.
I think to apply it to all the flash messages you need to put it in foundation.alert.js

Try this
setTimeout(function() { $(".alert-box a.close").trigger("click.fndtn.alert"); }, 1000);

Related

DRY view for sidebar

In my quest to keep my application views as DRY as possible I've encountered a little snag. My appliation.html.erb incorporates a static sidebar menu. Each of my main controllers incorporates a secondary sidebar menu (essentially a submenu). I can take the code that renders the menu out of application.html.erb and put it in each of my views and change the secondary sidebar there, but this produces a lot repetition in my views.
I saw this SO post and looked at this page, but I was unable to get either idea to work. I was thinking that I could put something like:
<% provide(:submenu, 'layouts/sidebars/sidebar_customers_contacts') %>
at the top of each view and use that to render the associated partial by doing
<% content_for(:submenu) do %>
<%= render :partial => :submenu %>
<% end %>
from the application.html.erb but of course that didn't work.
This is my current application.html.erb:
<div class="side">
<%= render 'layouts/sidebar' %>
<%= render 'layouts/sidebars/sidebar_dashboard' %><!-- this needs to load a sidebar based on the controller that calls it. Each view of the controller will get the same sidebar. -->
</div>
<div class="main-content">
<%= yield %>
</div>
I feel like I'm making this more difficult than it really is. Is there a simple way to do this?
Rails provides a helper called controller_name which you can read more about here.
Assuming you adhere to your own naming conventions, this should work as-is. If you decide some controllers don't get a sidebar, you may need to throw in some conditionals...
application.html.erb
<div class="side">
<%= render "layouts/sidebar" %>
<%= render "layouts/sidebars/#{ controller_name }" %>
</div>
<div class="main-content">
<%= yield %>
</div>
EDIT
Sorry, my mistake was using single quotes instead of double-quotes. You cannot use #{string interpolation} within single quotes. Source

why does the script affect everything on my Rails 3 app even when cased in this code?

I have a third party script which is
<script type="text/javascript" src="http://thirdpartysite.com/front.asp?id=xxxx"></script>
What the script does is put a watermark on an image to show that it is copyrighted.
This is the code that I'm using in view, but no matter what, the script applies to all posts
<% if post.copyright == true %>
<script type="text/javascript" src="http://thirdpartysite.com/front.asp?id=xxxx"></script>
<% else %>
<% end %>
When I test it using text only, it works correctly
<% if post.copyright == true %>
Sample text here only applies to post where copyright==true
<% else %>
<% end %>
How can I get the script to only apply to certain posts?
After your clarified in a comment
Yeah, including javascript will apply pagewide if nothing else is specified in the javascript.
You need to do something like:
<div class="post <%= 'copyright' if post.copyright%>">
<img src="somesrc.jpg"></img>
</div>
and then apply the javascript only to the css selector 'div.copyright img'. Something along the lines of that. Depends on what the javascript supports or if you can change the source.

Change Devise Errors

In my application.html.erb file I have
<% flash.each do |key,msg| %>
<div class="message" id="<%= key %>">
<%= content_tag :div, msg %>
</div>
<% end %>
which shows all the notices. Is there a way to add Devise's error be viewed in that notice?
Also, is there a way I can edit the HTML code when there is an error.
When there is an error, it adds a div with an id field_with_errors — can I change that?
I don't know if it's what your looking for, but the config/locales/devise.en.yml allows you to change the content of the messages.
rails generate devise:views will copy Devise's views to your app so you can edit them freely.
https://github.com/plataformatec/devise#configuring-views

How to use an RoR application template and still be able to add things to the html head?

I'm learning RoR through this tutorial. In tute, an application.html.erb file is created in views/layouts. In the body, a content div is created, and the <%= yield %> line is used. The other views that are created are then just inserted into the content div in the body of the application template.
But there are a few views that I have where I need to add some javascript to the head.
My question is, how can I use this structure, with a central application template, but also be able to put code into the head of the document? Thanks for reading.
Add <%=yield :header %> in your HTML head. Then, in a view:
<% content_for :header do %>
<!-- Javascript here //-->
<% end %>
<!-- Rest of the page here //-->
The bit in the content_for block will render where your yield :header is in your template.

Netbeans Source Format: Can It Recognize Other Stuff? (For RoR Views)

I am in love with Netbeans Source->Format feature. Is there any way to get it to recognize stuff in these blocks
<% content_for :style do %>
<% end %>
as CSS? It would also help for autocomplete.
Ditto, of course, for
<% content_for :javascript do %>
<% end %>
blocks.
I'm not certain, but I think the source formatting is on a file basis only, not on parts of a file.
Edit: Actually, I'm wrong, the formatting can recognize various languages in the same file...obviously what's happening in an ERB file for ruby and html code. But I don't see a way of extending that.

Resources