Named routes: Rails $ rake routes Prefix Verb URI Pattern Controller#Action todos GET /todos(.:format) todos#index POST /todos(.:format) todos#create new_todo GET /todos/new(.:format) todos#new edit_todo GET /todos/:id/edit(.:format) todos#edit todo GET /todos/:id(.:format) todos#show PATCH /todos/:id(.:format) todos#update PUT /todos/:id(.:format) todos#update DELETE /todos/:id(.:format) todos#destroy
Controller: index action # app/controllers/todos_controller.rb def index @todos = Todo.all end # web/controllers/todo_controller.ex def index(conn, _params) do todos = Repo.all(Todo) render(conn, "index.html", todos: todos) end
Model in Phoenix # web/models/todo.ex defmodule PhoenixApp.Todo do use PhoenixApp.Web, :model schema "todos" do field :content, :string field :done, :boolean, default: false timestamps end @required_fields ~w(content done) @optional_fields ~w() def changeset(model, params \\ :empty) do model |> cast(params, @required_fields, @optional_fields) end end
Ecosystem • Ruby/Rails -> Rubygems • many gems • Elixir/Phoenix -> Hex • few • potential to increse • Hex is easy to use • http://qiita.com/ma2ge/items/0e19bf3f03078f589096
code # app/controllers/todos_controller.rb def index @todos = Todo.all end # web/controllers/todo_controller.ex def index(conn, _params) do todos = Repo.all(Todo) render(conn, "index.html", todos: todos) end
Performance: production $ ab -n 500 -c 100 localhost:4000/todos # Rails same as single request mode, because WebRick server # Phoenix Requests per second: 1291.92 [#/sec] (mean)
Conclusion/IMO • Elixir/Phoenix is not only for middleware but also web application • Phoenix has potential to switch current Rails application • Performance • Targeting developer happiness
Resources: Performance • Elixir vs Ruby Showdown - Phoenix vs Rails • http://www.littlelines.com/blog/2014/07/08/elixir-vs- ruby-showdown-phoenix-vs-rails/ • Benchmarking Phoenix vs Rails vs Sinatra vs Express vs Martini... • https://github.com/mroth/phoenix-showdown