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"?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With