Tilo
Tilo

Reputation: 33742

What's the most minimalistic way to render "OK" in Elixir/Phoenix?

In Rails you can render text directly, e.g. render :text => 'OK'

Is there a shortcut in Elixir/Phoenix to render text directly, without having to define a template or layout?

The shortest way I found was this:

  defmodule MyApp.PageController do
    use MyApp.Web, :controller

    def index(conn, _params) do
      # the file ok.html.eex contains just the string OK
      render conn, "ok.html", layout: false
    end
  end

Is there a shorter way to render "OK", without having to provide the template file "ok.html"?

Upvotes: 22

Views: 8294

Answers (2)

Manford Benjamin
Manford Benjamin

Reputation: 397

This is how I render text to check if my route is working before using a template.

def show(conn, _params) do
  text conn, "Display OK"
end

Upvotes: 1

user94559
user94559

Reputation: 60153

From http://www.phoenixframework.org/docs/controllers:

Rendering

Controllers have several ways of rendering content. The simplest is to render some plain text using the text/2 function which Phoenix provides.

Let's say we have a show action which receives an id from the params map, and all we want to do is return some text with the id. For that, we could do the following.

def show(conn, %{"id" => id}) do
  text conn, "Showing id #{id}"
end

Upvotes: 44

Related Questions