Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notices and errors on rails3

I read somewhere that rails 3 form helper does not have error messages embedded in it anymore. I am wondering how I am supposed to show flash messages when I set them up inside my controller or as an inline notice in redirect_to? How am I supposed to display them on my view? Is there helper for this?

For example if I have

def update
  if @person.save
    flash[:notice] = "Successfully saved!"
  end
end

how do i show the notice on my view?

like image 361
denniss Avatar asked Dec 25 '10 02:12

denniss


2 Answers

flash will still work as long as you display it in your layouts:

<div id="page">
  <% if flash[:alert] %>
    <p class="flash-error"><%= flash[:alert] %></p>
  <% end %>
  <% if flash[:notice] %>
    <p class="flash-notice"><%= flash[:notice] %></p>
  <% end %>
  <%= yield %>
</div>

You can either display error messages manually or use the dynamic_form gem which gives you the old behavior.

like image 141
Brian Deterling Avatar answered Oct 19 '22 03:10

Brian Deterling


You can still display flash messages in your view with this:

<%= flash[:notice] %>

But if you want to display for error messages:

  #In your form
  <%= form_for @foo do |f| %>
    <%= render "shared/error_messages", :target => @foo %>
    ...
  <% end %> 


#shared/_error_messages.html.erb
<% if target.errors.any? %>
<div id="error_explanation">
  <ul>
  <% target.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
<% end %>
like image 8
Joshua Partogi Avatar answered Oct 19 '22 05:10

Joshua Partogi