簡體   English   中英

rails3上的注意和錯誤

[英]notices and errors on rails3

我在某處讀到Rails 3 form helper不再嵌入錯誤消息。 我想知道當我在控制器中設置Flash消息或在redirect_to中作為內聯通知時如何顯示Flash消息嗎? 我應該如何在我的視圖上顯示它們? 有幫手嗎?

例如,如果我有

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

如何在我的視圖上顯示通知?

只要您在布局中顯示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>

您可以手動顯示錯誤消息,也可以使用dynamic_form gem來顯示舊行為。

您仍然可以通過以下方式在視圖中顯示即顯消息:

<%= flash[:notice] %>

但是,如果要顯示錯誤消息:

  #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