简体   繁体   English

rails3上的注意和错误

[英]notices and errors on rails3

I read somewhere that rails 3 form helper does not have error messages embedded in it anymore. 我在某处读到Rails 3 form helper不再嵌入错误消息。 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? 我想知道当我在控制器中设置Flash消息或在redirect_to中作为内联通知时如何显示Flash消息吗? 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? 如何在我的视图上显示通知?

flash will still work as long as you display it in your layouts: 只要您在布局中显示Flash,Flash仍将起作用:

<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. 您可以手动显示错误消息,也可以使用dynamic_form gem来显示旧行为。

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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM