Change Devise Errors - ruby-on-rails

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

Related

How to include conditional in mailer layout for mailer method?

I am designing a mailer layout in Rails. It's a general layout that different mailers will use. In the middle of the general layout, there's a link I want to include but only if it's user_mailer.rb with the method def account_activation(user) that is sending email. Is there a way to do this?
In the layout I tried:
<% if mailer_name == 'user' && ['account_activation'].include?(action_name) %>
but this generated the error:
undefined local variable or method 'mailer_name' for #<#<Class:0x007f18d0701f30>:0x00000006109c70>
If I change mailer_name to mailer it no longer generates an error but it doesn't display the link either (even though I'm looking at the preview for the account_activation method.
In your layout, in the footer section put something like this:
<div id='footer'>
copyright | terms | privacy
<% if content_for?(:footer) %>
|
<%= yield :footer %>
<% end %>
</div>
Then in your specific mailer view do this:
<% content_for :footer do %>
special link
<% end %>
I do this to add special notes in the footer of emails that vary from email to email. Works great.

How to Hide Foundation flash message?

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);

Rails 4: Flash message persists for the next page view

I am using the following code in my layout to display two types of flash messages:
<% if !flash[:notice].nil? %>
<div class="row">
<div class="flash notice col-xs-12">
<%= flash[:notice] %>
</div>
</div>
<% end %>
<% if !flash[:error].nil? %>
<div class="row">
<div class="flash error col-xs-12">
<%= flash[:error] %>
</div>
</div>
<% end %>
<%= debug(flash[:notice]) %>
<%= debug(flash[:error]) %>
They both work fine, but whenever one is triggered, it will still appear for one additional page view. I'm not using any caching gems.
Why is this happening? And how do I fix it?
Use flash.now instead of flash.
The flash variable is intended to be used before a redirect, and it persists on the resulting page for one request. This means that if we do not redirect, and instead simply render a page, the flash message will persist for two requests: it appears on the rendered page but is still waiting for a redirect (i.e., a second request), and thus the message will appear again if you click a link.
To avoid this weird behavior, when rendering rather than redirecting we use flash.now instead of flash.
The flash.now object is used for displaying flash messages on a rendered page. As per my assumption, if you ever find a random flash message where you do not expect it, you can resolve it by replacing flash with flash.now.

a way to dismiss the notice flash message in rails 3.2

is there a way to dismiss the standard rails 3.2 forum notice when created a record or logged in with devise? like in twitter-bootstrap there's a cross you can click to dismiss the notice message.
I hope there is a similar way in standard forms.
You asked if there was a simpler way. There is no built in way to do this.... You will need to do something like this:
<% if flash[:notice] %>
<p class="notice"><%= flash[:notice] %></p>
<% end %>
<% if flash[:error] %>
<p class="error"><%= flash[:error] %></p>
<% end %>
In your app/assets/javascripts/flash.js.coffee
$ ->
$(".notice, .error").on("click", (event)->
$(event.target).hide("slow")
)
This will make it where if you click on the .notice or .error, it will hide the slowly (fade out). To this you can include a X icon to close it.
As I understand you not want notice on any particular action so delete <%= notice%> from application.rb file from your layout folder in view.
I hope i understood your question correctly.

In RoR, is there an easy way to prevent the view from outputting <p> tags?

I'm new to Ruby and Rails and I have a simple controller that shows an item from the database in a default view. When it is displaying in HTML it is outputting <p> tags along with the text content. Is there a way to prevent this from happening? I suppose if there isn't, is there at least a way to set the default css class for the same output in a statement such as this:
<% #Items.each do |i| %>
<%= i.itemname %>
<div class="menu_body">
Link-1
</div>
<% end %>
So the problem is with the <%= i.itemname %> part. Is there a way to stop it from wrapping it in its own <p> tags? Or set the css class for the output?
Thanks!
You need to enclose it with the HTML tag of your choice. Also if required you can escape bad code by using <%=h i.itemname %> Example:
<% #Items.each do |i| %>
<div><%=h i.itemname %></div>
<div class="menu_body">
Link-1
</div>
<% end %>
Edit: Ryan Bigg is right. Rails doesn't output a <p> tag. Sorry for the wrong info.
You canchange the public/stylesheets/scaffold.css if you want.
Or if you want to change it for a single page say items/index.html.erb
<style>
p{
/* your style here *?
}
</style>

Resources