phoenix-framework

How to handle associations and nested forms in Phoenix framework?

泄露秘密 提交于 2019-12-31 10:07:47
问题 What is the way to handle associations and nested forms in Phoenix framework? How would one create a form with nested attributes? How would one handle it in the controller and model? 回答1: There is a simple example of handling 1-1 situation. Imagine we have a Car and an Engine models and obviously a Car has_one Engine . So there's code for the car model defmodule MyApp.Car do use MyApp.Web, :model schema "cars" do field :name, :string has_one :engine, MyApp.Engine timestamps end def changeset

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

*爱你&永不变心* 提交于 2019-12-31 08:54:16
问题 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 def index(conn, _params) do log("this text") # ... end end 回答1: Okay, turns out it's pretty straight forward. You need to require the Logger elixir module in your controller and call one of it's methods to log your text. defmodule PhoenixApp.TopicController do require Logger def index(conn, _params) do

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

天涯浪子 提交于 2019-12-31 08:53:55
问题 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 def index(conn, _params) do log("this text") # ... end end 回答1: Okay, turns out it's pretty straight forward. You need to require the Logger elixir module in your controller and call one of it's methods to log your text. defmodule PhoenixApp.TopicController do require Logger def index(conn, _params) do

Phoenix fixture json file

ぃ、小莉子 提交于 2019-12-31 04:00:05
问题 How can I load a JSON fixture file in my phoenix project? When I tried something like Application.app_dir(my_app, "priv"), it gives me a compiled path and I can't use that on my tests . Is there any other way to load a fixture file from "test/support/somefile.json" ? 回答1: You can use __DIR__ with Path.expand/2 . For example, if your tests are in test/controllers/page_controller_test.exs , you can get the path to test/support/somefile.json using: path = Path.expand("../support/somefile.json",

Elixir/Phoenix restrict params like Rails strong params

眉间皱痕 提交于 2019-12-31 02:23:06
问题 I am making an API only Phoenix app. I come from a Ruby on Rails background, so bear with me. Say I have a User model with email , password , password_hash , and role fields. I need to restrict the role and password_hash fields from user input, or whitelist the email and password fields. Right now anyone could POST this sign up as an admin: { "user": { "email": "test3@test.com", "password": "testpw", "password_hash": "shouldn't allow user input", "role": "admin" } } This is typically

Phoenix setup failing: Compilation error, (KeyError) key :model not found

时光怂恿深爱的人放手 提交于 2019-12-25 09:25:06
问题 I have postgres running and can connect to it, but a brand new phoenix app via mix phoenix.new reporting . I can not complete setup, can not do ecto.create and the best I can tell is that it didn't connect to the database... or maybe it's some other bug... bascially, I don't know where to go from here. $ psql -p 5432 -h localhost reporting_dev postgres Password for user postgres: psql (9.6.1, server 9.6.2) Type "help" for help. reporting_dev=# And here is my config/dev.exs $ grep Reporting

Elixir/Phoenix map for the channels payload

匆匆过客 提交于 2019-12-25 07:18:18
问题 Well, I am looking for a good way to pass multiple elements (same column name) that I've retrieved from db into the channels payload. For example: ppl = Repo.all(People) will return two results with id: 1, name: Mike , id: 2, name: John . The name: (column name) is used for both Mike and John, but when passing through channels payload, I can only pass one map, where can't have both name: John, name: Mike at the same time. As I understood channels, we use a map(payload) that is send back to

Failed to connect to Database - Setting up GCP app engine

北城余情 提交于 2019-12-25 02:57:12
问题 I'm trying to setup an instance on my phoenix app. I'm actually able to generate the app but I'm getting an error when trying to connect to the DB: ERROR: 21:11:31.017 [error] Postgrex.Protocol (#PID<0.2223.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (/tmp/cloudsql/statcasters:us-central1:statcastersdb): no such file or directory - :enoent I'm not sure what is wrong? The file seems to exist but it seems to say it doesn't: prod.secret.exs: config :statcasters,

Presence not picking up user leave events?

匆匆过客 提交于 2019-12-24 18:50:30
问题 I need to perform some actions when the user leaves a channel (in most cases where they close the tab voluntarily, but there may also be a connection loss/timeout etc.) According to posts like https://elixirforum.com/t/phoenix-presence-run-some-code-when-user-leaves-the-channel/17739 and How to detect if a user left a Phoenix channel due to a network disconnect?, intercepting the "presence_diff" event from Presence seems to be a foolproof way to go, as it should also covers the cases where

How do I point to a local elixir version in mix.exs file?

冷暖自知 提交于 2019-12-24 10:55:34
问题 So I have a phoenix project I'm using as a test app of sorts. If I want to use a local version of elixir source code that I'm using (and making changes on), how do I tell phoenix to use it? I also want to be able to use "iex -S mix" with it. The below elixir: ["~> 1.6.0-dev", path: '/my/local/path/to/elixir'] does not work, but it's what I'm trying to do. I already tried adding the below and I get a syntax error. Adding elixir to the dependencies (in the def deps function) didn't seem to do