1
votes

Action Controller Overview — Ruby on Rails Guides say

The flash is a special part of the session which is cleared with each request.

When flash is clear? What methods make new request?

I tryed redirect_to, render, link_to.


redirect_to -> show then clear.
render -> show then not clear. (So use flash.now)
link_to -> show then not clear.(So use flash.now)

So I believe redirect_to make new request.
redirect_to is all? other method make new request?

3

3 Answers

1
votes

redirect_to causes a new request. It tells the browser to send a new request for a different URL.

render can be used in many ways, but it's mostly used to render the HTML views to the browser, in other words, it does the heavy lifting of rendering your application's content for use by a browser:

render "some_view"

in this case there is not HTTP request involved. But, there are other ways that you can use render, e.g. rendering an action:

render :action_name, notice: "message"

in this use case of render there is a HTTP request involved.

link_to is a helper method of ActionView::Helpers::UrlHelper that is used in your view, and generates HTML code for a given link. It also makes a new HTTP request to your browser in most of the cases. The exception is an Ajax request.

1
votes

Sessions

The flash is just part of Rails' session cookie system:

The flash is a special part of the session which is cleared with each request

The session is a cookie stored on the client system.

It is a hash ({key: "value"}), which means you can store a number of different pieces of data inside it.


Rails reserves the flash inside the session cookie (it can only be populated through your controllers). You can populate the various elements of the flash session -- it basically looks like this:

session: {
   flash: {
      notice: ""
      error: ""
      alert: ""
   }
}

This is corroborated here:

@env[Flash::KEY] ||= Flash::FlashHash.from_session_value(session["flash"])

In its nature, the session variable is temporary, as described here:

Anything you place in the flash will be exposed to the very next action and then cleared out

This means that each time your application serves a new request (IE the user reloads their browser), the flash will be updated.

Remember, the session is a cookie stored on the clients' system. Client systems can only be updated through a complete HTTP refresh.

--

To answer your question, it depends on how you're using render.

If render forms part of an HTTP refresh, the flash will be updated. If it is just to add a partial, it won't change the flash.

redirect_to is self explanatory. link_to invokes a new HTTP request, so the flash will be updated (unless it's bound to ajax).


Render

In the controller, render is often used to describe the "rendering" of a new action:

def create
   render :new, notice: "New action"
end

Used in this context, the render action will invoke the same process as redirect_to -- pushing the browser to load a new url.

0
votes

Check this link to see examples:

what is the difference between link_to, redirect_to, and render?

There is few ways to use in a controller, Example:

  def update
    if @contact.update
      flash[:success] = 'The "@contact.title.capitalize" updated Successfully'

      #redirect_to :action => 'index', togo back to contact index page. 
      #redirect_to root_path, togo back to root index page
      redirect_to contact_path #
    else
      # flash pops and renders back to edit page.
      flash[:danger] = "Ouch smth went wrong..."
      render "edit"        
    end
  end