Is there a way to add a class or an id to a specific flash message? I need to have some messages fade away and some persist. I would like to do this based on a html class.
Thanks for any help
There is nothing magical about what can put in the flash. It's just a hash that is stuffed in the session and cleared out for you.
You could (for example) do
flash[:notice] = {:class => :urgent, :body => 'hello'}
And then in your layout
- if flash[:notice]
%div{:class => flash[:notice][:class]}
= flash[:notice][:body]
But there's definitely more than one way to do this - you could just use one class when it's flash[:notice], another for flash[:error] etc
Flash messages are simply stored in a hash, flash. In your view, you might iterate over all of your flash messages like this:
<% flash.each do |key, msg| %>
<%= content_tag :div, msg, :id => key %>
<% end %>
You could always check for a specific message and append a specific class. Maybe something like:
<% flash.each do |key, msg| %>
<% if msg.include? 'fatal' %>
<%= content_tag :div, msg, :id => key, :class => 'fatal' %>
<% end %>
<% end %>
Here's a railscast with more information on flash messages.
http://railscasts.com/episodes/18-looping-through-flash
Add a class:
<%= content_tag(:p, notice, class: 'notice') if notice %>
<%= content_tag(:p, alert, class: 'alert' ) if alert %>
Add an id:
<%= content_tag(:p, notice, id: 'notice') if notice %>
<%= content_tag(:p, alert, id: 'alert' ) if alert %>
It renders (example):
<p class="alert">Invalid email or password.</p>
<p id="alert">Invalid email or password.</p>
Related
I have a dropdown in the form i'm using
<%= f.select(:visitable_id) do %>
<% #visitors.each do |visitor| %>
<%= content_tag(:option, visitor.name, value: visitor.id, data: {:type => visitor.class.name.to_s} ) %>
<% end %>
<% end %>
When submitted I get the visitable_id in the controller. But I would also like to receive the data of the data value in the content_tag. The: data: {:type => visitor.class.name.to_s}
How can you access the second value in the controller? Any help would be appreciated!
HTML5 data attributes are not sent with the form. Nor is anything else except the name and value of the inputs.
You could solve it by passing both values through a single option.
<%= f.select(:visitable_id) do %>
<% #visitors.each do |visitor| %>
<%= content_tag(:option, visitor.name, value: "#{visitor.class.name}=visitor.id") %>
<% end %>
<% end %>
You can then parse them out by splitting the string:
klass, id = params[:visitable_id].split('=')
But make sure you whitelist the acceptable values for klass to avoid a vulnerability where a malicious user can query any model.
I'm making rails + Devise + Bootstrap app.
Basically it seems like working well, but when it gets wrong password
, no error message is shown like this.
I can't figure out why it happened and how to fix it.
Could you give me any advise for that? Thanks in advance!
Add these lines above the yield block in your application layout as suggested in one comment.
# In application.html.erb
<% flash.each do |name, msg| %>
# New code (allow for flash elements to be arrays)
<% if msg.class == Array %>
<% msg.each do |message| %>
<%= content_tag :div, message, :id => "flash_#{name}" %>
<% end %>
<% else %>
# old code
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %> #don't forget the extra end
<% end %>
Just add this code in the controller action, where you want to show the error messages,
flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages
This link gives you many answers for this
I am using devise for log in and i have overwrite my devise session controller and showing flash messages if user enter wrong information using following code to display flash message.
<% flash.each do |name, msg| %>
<%= content_tag :section, msg, :id => "flash_#{name}", :class => " flash" %>
<% end %>
And it is working fine and showing "Invalid email or password." message if user enter wrong email and password .
Now it is still showing the same flash message if i refresh the page.
But i want the flash message to be remove if page gets refresh .please help me on same.
You can use flash.discard after it getting displayed. Possibly like:
<% flash.each do |name, msg| %>
<%= content_tag :section, msg, :id => "flash_#{name}", :class => " flash" %>
<% flash.discard(name) %>
<% end %>
I haven't tested this, so try if it works.
A am new to devise, and just set it up with default functionality.
I cannot get the flash[:notice] messages to show up.
I put this in my application.html.erb file
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
I can get the error messages like "already logged in" but I cannot get the regular notice messages like "Signed in successfully."
Can anyone point me in the direction of why this is not working.
I just want to use the defaults I dont care about having custom notices.
Try the change below.
<%= content_tag :div, msg, :id => name %>
On my site, the user can watch his profile. In his profile he can have a look at his data (i.e. signature).
Now I want my users being able to edit this data while watching it.
So I coded the following in my view:
<div id="profile-signature">
<p>
<b>Signature:</b>
<%=h #user.signature %>
</p>
<%= form_remote_tag(:update => "signature",:url => { :action => :update_signature }) %>
<%= text_area(:signature,:class=>"form-textarea") %>
<%= submit_tag "Save Signature" %>
</div>
in my user controller, I created a new action update_signature
def update_signature
puts 'in function!'
#user = current_user
puts #user.login
puts params[:signature]
#user.signature = params[:signature]
#user.save
puts 'saved'
end
Now, submitting the form, puts params[:signature] will output: classform-textareasfsffsfs
where sfsffsfs is the text I entered.
Reloading and my page and output the signature on page (<%=h #user.signature %>), I get:
"--- !map:HashWithIndifferentAccess classform-textarea: sfsffsfs "
Why do I get this strange string instead of just sfsffsfs (in this case)?
What to do, to update the data (<%=h #user.signature %>) automatic without page reload?
Use text_area_tag for getting the text_area field values . With reloading the page there is a mismatch in the div id it should be signature not profile-signature .
<div id="profile-signature">
<p>
<b>Signature:</b>
<%=h #user.signature %>
</p>
<%= form_remote_tag(:update => "signature",:url => { :action => :update_signature }) %>
<%= text_area(:signature,:class=>"form-textarea") %>
<%= submit_tag "Save Signature" %>
</div>
Make the following changes
<div id="signature">
<p>
<b>Signature:</b>
<%=h #user.signature %>
</p>
<%= form_remote_tag(:update => "signature",:url => { :action => :update_signature }) %>
<%= text_area_tag(:signature,:class=>"form-textarea") %>
<%= submit_tag "Save Signature" %>
</div>
Hope this helps !
It looks like your text_area call isn't quite right, looking at the docs it should be like this:
text_area(object_name, method, options = {})
so your css class is being set as the method, instead you should use text_area_tag:
<%= text_area_tag(:signature, #user.signature, :class=>"form-textarea") %>
Then the correct value (the text in the text area) should be submitted as the params you're expecting.