Ruby page error prompt flash

Source: Internet
Author: User

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

How to display the error message in controller on the page,

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 is a special part of the session and is cleared every time a request is sent. This means that the stored data is valid only during the next request. this feature is widely used to store error message prompts. The method for obtaining it is similar to session, similar to hash. let's take exit as an example. The information sent by the Controller will be displayed in the next request.

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.

Note: You can also specify the flash information during page redirection.

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

TheDestroyAction redirects to the application'sRoot_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:

The logout operation will redirect the page to the root directory, and the information will also be displayed on the page. this depends entirely on what the next step is. In short, the information of the last request will be stored in flash, and the final error information or prompt information will usually be displayed on the page.

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

In this way, if an error message or notification occurs during the operation, it is automatically loaded to the page.

If you want a flash value to be carried over to another request, useKeepMethod:

If you want to bring the prompt information to other requests, use the keep method.

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, ifCreateAction fails to save a resource and you renderNewTemplate 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. NowIn the same way you use the normalFlash:

By default, values in flash will be valid for the next request, but sometimes you may want to use these values in the same request, for example, if the data fails to be saved during the creation and jumps to a new page, flash will not appear in this new request. if you still want to display the error information through flash, you can directly use flash. now method.

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

Bytes -----------------------------------------------------------------------------------

 

Welcome to my blog. I am a small screw in the Internet industry-an atypical programmer sister.

Leave an address for you to contact me

 

Ruby page error prompt flash

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.