How to redirect back to previous page in Elixir & Phoenix?

In this post, you'll learn how to easily redirect users to the previous path using the Navigation History plug.

Table of contents

    It's super easy, first add the plug to dependencies:

    def deps do
      [ 
        ...,
        {:navigation_history, "~> 0.3"}]
    end

    Then, add it to your pipeline of choice (in most cases it's gonna be :browser):

    pipeline :browser do
      ...
      plug NavigationHistory.Tracker
    end

    Now, you can use use available NavigationHistory.last_path/1 function to access the previous path:

    redirect(conn, to: NavigationHistory.last_path(conn))

    That's it! You can also exclude specific paths from navigation history:

    plug NavigationHistory.Tracker, excluded_paths: ["/login", ~r(/admin.*)]

    Find more details about this lib here: https://github.com/danhper/plug-navigation-history.

    Until next time!

    Download our ebook
    Szymon Soppa Web Developer
    Szymon Soppa Curiosum Founder & CEO

    Read more
    on #curiosum blog

    7 Reasons Why You Might Need a Custom Marketing Tool

    Managing a marketing agency or introducing a company to digital transformation in the area of marketing, requires an extraordinary focus on the tools that you should use. Usually, they facilitate the work of marketers and allow you to achieve the expected results of each project and advertising campaign you are working on.

    How to upload a file in Elixir with Waffle

    The ability to upload files is a key requirement for many todays web and mobile applications. In this tutorial, we will look at how we can accomplish file uploads to local storage and S3 server in Phoenix with the help of Waffle library.