Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"?

like image 319
Tilo Avatar asked Jul 26 '16 23:07

Tilo


2 Answers

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
like image 96
user94559 Avatar answered Nov 07 '22 19:11

user94559


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
like image 1
Manford Benjamin Avatar answered Nov 07 '22 20:11

Manford Benjamin