Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>HTTP 404 Status</h2> <p>To return a 404 header, just use the <code>:status</code> option for the render method.</p> <pre><code>def action # here the code render :status =&gt; 404 end </code></pre> <p>If you want to render the standard 404 page you can extract the feature in a method.</p> <pre><code>def render_404 respond_to do |format| format.html { render :file =&gt; "#{Rails.root}/public/404", :layout =&gt; false, :status =&gt; :not_found } format.xml { head :not_found } format.any { head :not_found } end end </code></pre> <p>and call it in your action</p> <pre><code>def action # here the code render_404 end </code></pre> <p>If you want the action to render the error page and stop, simply use a return statement.</p> <pre><code>def action render_404 and return if params[:something].blank? # here the code that will never be executed end </code></pre> <h2>ActiveRecord and HTTP 404</h2> <p>Also remember that Rails rescues some ActiveRecord errors, such as the <code>ActiveRecord::RecordNotFound</code> displaying the 404 error page.</p> <p>It means you don't need to rescue this action yourself</p> <pre><code>def show user = User.find(params[:id]) end </code></pre> <p><code>User.find</code> raises an <code>ActiveRecord::RecordNotFound</code> when the user doesn't exist. This is a very powerful feature. Look at the following code</p> <pre><code>def show user = User.find_by_email(params[:email]) or raise("not found") # ... end </code></pre> <p>You can simplify it by delegating to Rails the check. Simply use the bang version.</p> <pre><code>def show user = User.find_by_email!(params[:email]) # ... end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload