FAQ Database Discussion Community


In Elixir/Phoenix, after template change, “cannot define module MyApp.PageView because it is currently being defined”

elixir,phoenix-framework
I just changed an image path in a template in my Phoenix app and reloaded the page. This error message appeared in the browser: CompilationError at GET / Showing console output == Compilation error on file web/views/page_view.ex == ** (CompileError) web/views/page_view.ex:1: cannot define module Youli.PageView because it is currently being...

Pushing operations to a message queue for processing when able to

elixir,phoenix-framework
My title isn't great because I'm finding it hard to explain what I want in a small snippet...soo I'll try in depth! I'm writing an Elixir app using the Phoenix web framework. What I want to do is this: Call a web route, which phoenix handles and passes off to...

How can I handle the empty case in Elixir/Phoenix templates?

elixir,phoenix-framework
In a template I have an iteration over some days in an itinerary: <%= for day <- @days do %> <tr> <td><%= day.number %></td> <td><%= day.main_event_time %></td> <td><%= day.main_event %></td> <td><%= day.location %></td> <td><%= day.who %></td> <td><%= day.summary %></td> <td><%= day.lodging %></td> <td><%= link "Show", to: day_path(@conn, :show, day) %></td>...

How to handle deprecation of do…end in Elixir/Phoenix after v0.9.0?

elixir,phoenix-framework
I was using Phoenix v0.9.0 until today. To get a bug fix I switched to the master branch, but now this warning appears while running mix compile: warning: using do...end in views is deprecated, please use the new YourApp.Web definitions web/view.ex:6: Youli.View (module) I'd like to do that. How can...

Setting up Phoenix Framework and Ecto to use UUIDs: how to insert the generated value?

postgresql,elixir,phoenix-framework,ecto
A few days ago I started using Elixir and the Phoenix Framework (v 0.12.0) with a Postgres database. I'm trying to create a table which has a UUID primary key, which I prefer over the sequential default. After using mix phoenix.gen.html to generate the model and migration files and following...

Which OTP behavior should I use for an “endless” repetition of tasks?

elixir,otp,phoenix-framework
I want to repeatedly run the same sequence of operations over and over again next to a Phoenix application (without crashing the whole web-app if something brakes in the worker of course) and don't really know wether I should use a GenServer, Elixir's Tasks, an Agent or something completely different...

How to Log something in Controller when Phoenix Server is running?

elixir,phoenix-framework
Well, the question is pretty clear. I'm trying to print some debug information from one of my Controllers in my Phoenix App when the Server is running. defmodule PhoenixApp.TopicController do use PhoenixApp.Web, :controller alias PhoenixApp.Topic plug :action def index(conn, _params) do # ... log "this text" # ... render(conn, "index.html")...

Phoenix Framework before filter

elixir,phoenix-framework
Does the Phoenix Framework employ any types of callback filter such as those found in Rails? I know it is possible to validate changesets, but I am looking for ways to implement actions such as before_create, before_save and after_commit.

Querying by DateTime in Ecto

elixir,phoenix-framework,ecto
Here is what I have tried. date = Ecto.DateTime.from_erl(:calendar.universal_time()) query |> where([record], record.deadline >= ^date) I also tried date = Ecto.DateTime.from_erl(:calendar.universal_time()) query = from m in MyApp.SomeModel, where: m.deadline >= ^date, select: m Both return same message value `%Ecto.DateTime{..}` in `where` cannot be cast to type :datetime in query From...

Horribly redundant Phoenix controller

elixir,phoenix-framework,ecto
I'm doing my first Phoenix application, and trying to do new/2 controller. The code I wrote is def new(conn, %{"fbid" => fbid, "latitude" => lat, "longitude" => lng, "content" => content}) do {fbid, _} = Integer.parse(fbid); {lat, _} = Float.parse(lat); {lng, _} = Float.parse(lng); chats = %Chat{:fbid => fbid, :latitude...

How does Polymorphic association work with Ecto?

elixir,phoenix-framework
Ecto seems to support polymorphic association as I read through https://github.com/elixir-lang/ecto/issues/389 and its related issues linked from it. Let's say I need a Comment model association on Task and Event models. If my understanding of Ecto association with custom source is right, then we need four tables and three models,...

Ecto validation fails with no error

elixir,phoenix-framework,ecto
I'm trying to create a basic CRUD app, but I'm having problems with validation. here's my controller action: def update(conn, %{"id" => id, "user" => params}) do case Repo.get(User, String.to_integer(id)) do user when is_map(user) -> user = Map.merge(user, atomize_keys(params)) case User.validate(user) do [] -> Repo.update(user) redirect conn, to: user_path(:show, user.id)...

How to start phoenix.server as daemon

elixir,phoenix-framework
The below command run the phoenix.server in production mode and is given as the command for deploying phoenix. MIX_ENV=prod PORT=4001 iex -S mix phoenix.server However, the above command run the server interactively and closing the terminal, stops the phoenix.server from running. How to have phoenix.server run in the background? ...

How to override errors in phoenix?

elixir,phoenix-framework
I build restful api (json) on phoenix. And I did not need the support of html. How to override errors in phoenix? Example errors: - 500 - 404 when no route found and other....

Where can I put my Plugs and then use them from different controllers in my Phoenix app?

elixir,phoenix-framework
I'm creating my first Elixir-Phoenix app. I've written a few plugs that I want to use in multiple controllers, right now there is a lot of code duplication since the Plug code is being repeated in all of my controllers. My question is, is there a file where I can...

(RuntimeError) expected connection to have a response

elixir,phoenix-framework
I'm a new user to Phoenix Framework and I'm trying to set up a simple HTTP POST service, that performs a calculation on incoming data and returns the result but I'm getting the following error: ** (RuntimeError) expected connection to have a response but no response was set/sent stacktrace: (phoenix)...

How to delete a Phoenix Session?

elixir,phoenix-framework
I'm going through the Phoenix Guide on Sessions. It explains it very well how I can bind data to a session using put_session and fetch the value later using get_session but it doesn't tell how I can delete a User's session. From the guide: defmodule HelloPhoenix.PageController do use Phoenix.Controller def...

Phoenix: Broadcasting from IEx console

elixir,phoenix-framework
I have built a small chat app like the one here: https://github.com/chrismccord/phoenix_chat_example/blob/master/web/channels/room_channel.ex And cannot figure out how to broadcast to all users in a topic a message. In the above application (which isn't updated to v0.13 like I'm using), how would I do that? Below is what I've tried with...

Setting up custom response for exception in Phoenix Application

elixir,phoenix-framework,ecto
im writing phoenix application with ecto and have the following snippet in the test {:ok, data} = Poison.encode(%{email: "[email protected]", password: "mypass"}) conn() |> put_req_header("content-type", "application/json") |> put_req_header("accept", "application/json") |> post(session_path(@endpoint, :create), data) > json_response(:not_found) == %{} this throws a Ecto.NoResultsError i have this defined defimpl Plug.Exception, for: Ecto.NoResultsError do def...

“Cannot begin test transaction because we are already inside one”

elixir,phoenix-framework,ecto
I followed this tutorial and my simple test always fail with this error 1) test /index returns a list of contacts (WorldNote.ChatsControllerTest) test/controllers/chats_controller_test.exs:16 ** (RuntimeError) cannot begin test transaction because we are already inside one stacktrace: (ecto) lib/ecto/adapters/sql.ex:321: anonymous fn/6 in Ecto.Adapters.SQL.start_test_transaction/3 (ecto) lib/ecto/adapters/sql.ex:615: Ecto.Adapters.SQL.pool_transaction/4 (ecto) lib/ecto/adapters/sql.ex:314: Ecto.Adapters.SQL.start_test_transaction/3...

Set default value for date selector in phoenix framework to current date

elixir,phoenix-framework
In the app I'm developing I have a date selector which will mostly be used with the current date as value (or a date a few days later). In order to reduce work for my users, I want to set today's date as default value. I can easily set the...

Optional POST parameters in Elixir Phoenix

elixir,phoenix-framework
I have a phoenix route that I want to POST some form data to, however there are about 4 fields of the form that are optional (the form is constructed by the end user and therefore these fields may not exist in the POST payload) In the Phoenix controller for...

How to stub (or prevent running) of a call to a worker in my ExUnit test?

elixir,phoenix-framework
I have a Phoenix app (which is just a restful api with no front end) and one of the controllers does some stuff which I want to test, but at the end of the controller it calls a dispatcher which sends a payload off to a worker (run under poolboy)...

“an adapter was given to :pubsub but no :name was defined” with Elixir/Phoenix after v0.9.0

elixir,phoenix-framework
This error comes up when running mix compile after switching to Phoenix master after starting with v0.9.0: == Compilation error on file lib/youli/endpoint.ex == ** (ArgumentError) an adapter was given to :pubsub but no :name was defined, please pass the :name option accordingly lib/youli/endpoint.ex:2: (module) (stdlib) erl_eval.erl:657: :erl_eval.do_apply/6 I haven't...

Why Phoenix doesn't use Plug to start the server?

elixir,phoenix-framework
liveforeverx on irc has answered my original question and I have modified this question as a followup. Phoenix depends on Plug for many of its function. However, when it comes to starting cowboy server, why doesn't Phoenix start it using Plug's api in Phoenix.Endpoint.CowboyHandler.start_link? Why does start_link on ranch_listener_sup is...

Rails' before_filter equivalent in Phoenix

ruby-on-rails,elixir,phoenix-framework
I've just started working on my first Phoenix app, and the issue is that I have some common lines of code in every action in my controller, that I would like to separate out. These lines fetch data from multiple Ecto Models and saves them to variables for use. In...

Elixir/Phoenix: Missing beam file elf_format <<“/usr/lib/erlang/lib/hipe-3.11.2/ebin/elf_format.beam”

elixir,phoenix-framework
While trying to compile a release of my Phoenix project, the following error comes up: $ mix release .... ==> Generated .appup for myapp 0.0.1 -> 0.0.2 ===> Missing beam file elf_format <<"/usr/lib/erlang/lib/hipe-3.11.2/ebin/elf_format.beam">> could not find app plug {0,10,0} $ elixir --version Elixir 1.0.3 The Phoenix version is 0.10.0. Am...

How to PATCH or DELETE from a form without javascript

phoenix-framework
I am writing a simple crud app on Phoenix. How does one submit PATCH and DELETE requests from a form without using javascript?

Route to a static page in phoenix-framework

phoenix-framework
I want to run an angularJS front end with a phoenix backend for my site. I would like my root route to direct the user to a pre-built page in the static directory which contains my angular client and then use phoenix to run the API. I have done this...

How to do Elixir mixins

elixir,phoenix-framework
I'm trying to create a mixins for authentication login, so it can be applied to my models which should be able to login. Much like the the has_secure_password in Ruby. Afaik this is done using the use statement which essential requires the module, and calls the __using__ macro. So I...

Phoenix - controller with multiple render

elixir,phoenix-framework
Trying to create an app with Elixir + Phoenix, that would be able to handle both "browser" and "api" requests to handle its resources. Is it possible to do it without having to do something like that : scope "/", App do pipe_through :browser resources "/users", UserController end scope "/api",...