Ruby分頁錯誤提示flash

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   使用   

http://guides.rubyonrails.org/v3.0.8/action_controller_overview.html#the-flash

如何在頁面顯示controller裡的錯誤提示,

The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for storing error messages etc. It is accessed in much the same way as the session, like a hash. Let’s use the act of logging out as an example. The controller can send a message which will be displayed to the user on the next request:

flash是session特殊的一部分,每次請求的時候都會被清除,這意味著儲存的資料只有在下一次請求的過程中才有效.這一特性被廣泛的應用於儲存錯誤資訊提示,擷取它的方式與session近似,類似於雜湊.讓我們以退出為例子,controller發送的資訊將在使用者下次請求的時候展示出來.

class LoginsController < ApplicationController  def destroy    session[:current_user_id] = nil    flash[:notice] = "You have successfully logged out"    redirect_to root_url  endend

Note it is also possible to assign a flash message as part of the redirection.

注意:也可以在頁面重新導向的時候指定flash的資訊

redirect_to root_url, :notice => "You have successfully logged out"

The destroy action redirects to the application’s root_url, where the message will be displayed. Note that it’s entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It’s conventional to display eventual errors or notices from the flash in the application’s layout:

登出登入的操作會使得頁面重新導向到根目錄,這時候資訊也會顯示在頁面上.這完全取決於下一步操作是什麼,總之,上一次請求的資訊將會被存入flash,通常將會把最終的錯誤資訊或者提示資訊展示到頁面上.

<html>  <!-- <head/> -->  <body>    <% if flash[:notice] %>      <p class="notice"><%= flash[:notice] %></p>    <% end %>    <% if flash[:error] %>      <p class="error"><%= flash[:error] %></p>    <% end %>    <!-- more content -->  </body></html>

This way, if an action sets an error or a notice message, the layout will display it automatically.

通過這種方式, 如果操作過程中出現的錯誤提示或者通知,會自動的載入到頁面上

If you want a flash value to be carried over to another request, use the keep method:

如果你想把提示資訊帶到其他請求中使用,使用keep方法

class MainController < ApplicationController  # Let‘s say this action corresponds to root_url, but you want  # all requests here to be redirected to UsersController#index.  # If an action sets the flash and redirects here, the values  # would normally be lost when another redirect happens, but you  # can use ‘keep‘ to make it persist for another request.  def index    # Will persist all flash values.    flash.keep     # You can also use a key to keep only some kind of value.    # flash.keep(:notice)    redirect_to users_url  endend

By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the create action fails to save a resource and you render the new template directly, that’s not going to result in a new request, but you may still want to display a message using the flash. To do this, you can useflash.now in the same way you use the normal flash:

預設狀態下, flash裡的值會在下一次請求的時候有效, 但是有的時候,你可能希望在同一個請求裡使用這些值,比如建立的時候儲存資料失敗並跳轉到一個新的頁面裡,flash不會出現在這個新的請求裡.如果依然希望通過flash來顯示錯誤資訊,可以直接使用flash.now方法.

class ClientsController < ApplicationController  def create    @client = Client.new(params[:client])    if @client.save      # ...    else      flash.now[:error] = "Could not save client"      render :action => "new"    end  endend

-----------------------------------------------------------------------------------

 

歡迎來到我的部落格, 本人乃互連網行業一枚小小的螺絲釘—非典型程式員妹子.

留下地址,方便大家和我聯絡

 

Ruby分頁錯誤提示flash

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.