How to redirect to 404 page in Rails?

Small tutorial that will help you to redirect user to a "fake" 404 page, using Rails
31 October 2017   4345

Rails has this functionality built in already. If you want to show a 404 page, create a render_404 method (or not_found ) in ApplicationController like this:

def not_found
  raise ActionController::RoutingError.new('Not Found')
end

Rails also handles AbstractController::ActionNotFound, and ActiveRecord::RecordNotFound the same way.

This does two things better:

It uses Rails' built in rescue_from handler to render the 404 page, and 2) it interrupts the execution of your code, letting you do nice things like:

  user = User.find_by_email(params[:email]) or not_found
  user.do_something!

without having to write ugly conditional statements.

As a bonus, it's also super easy to handle in tests.

Ruby/RoR News Digest 5 - 11.10

Minitest style guide, why RSpec tests fail algorithms behind RuboCop complexity metrics and much more in this week's Ruby\Rails news digest
11 October 2019   118

Greetings! I hope your week went great! Here's new "gemmy" programming news digest.

In this digest you will be able to check the minitest style guide, learn how to troubleshoot the RSpec test fail and get familiar with algorithms behind Rubocop complexity metrics. Also, there are two videos with tips and tricks and system test creating in Rails

Guides

  • The Minitest Style Guide

Style guide for the popular Ruby testing library

Articles

  • Ruby 2.7 deprecates automatic conversion from a hash to keyword arguments

Ruby 3.0, however, will have ‘real’ keyword arguments separated from the idea of hashes instead of initial Ruby's legacy approach in this field; so the automatic conversion of a supplied hash into keyword arguments will yield a warning in Ruby 2.7.

  • Why RSpec Tests Fail (and How To Fix Them)

Examples of where RSpec tests could fail and thoughts on fixing such issues.

  • Algorithms behind RuboCop complexity metrics

Learn how RuboCop calculates its complexity metrics on code.

Updates

  • Spree

4th major version of the "complete open source ecommerce solution for Ruby on Rails"

  • will_paginate

Update of the pagination library that works with Ruby on Rails, Sinatra, Hanami::View, Merb, DataMapper and Sequel.

  • PunyLinux

Build automation  for a minimal Linux system.

Video

  • Episode #210 - Rails Tips and Tricks

  • How to Write System Tests in Rails

Podcasts

  • 290: Ruby for Good with Polly Schandorf

Brittany Martin, host of the 5by5 Ruby on Rails podcasts talks with Polly Schandorf is an organizer of Ruby for Good, an hackathon-of-sorts about using Ruby to make the world “gooder”