我使用Phoenix框架 - Step by Step,在15分钟内关注本教程Elixir博客
但我有一个问题,我不知道如何解决.我认为它与postgres有关.
我做了:
$ mix ecto.create
$ mix ecto.migrate
C:\Users\999\Documents\elixir_files\blog>mix ecto.create
** (RuntimeError) could not find executable `psql` in path, please guarantee it is available before running ecto commands
lib/ecto/adapters/postgres.ex:105: Ecto.Adapters.Postgres.run_with_psql/2
lib/ecto/adapters/postgres.ex:82: Ecto.Adapters.Postgres.storage_up/1
lib/mix/tasks/ecto.create.ex:32: Mix.Tasks.Ecto.Create.run/1
(mix) lib/mix/cli.ex:55: Mix.CLI.run_task/2
(stdlib) erl_eval.erl:669: :erl_eval.do_apply/6
(elixir) lib/code.ex:131: Code.eval_string/3
C:\Users\999\Documents\elixir_files\blog>mix ecto.migrate
[warning] the :size option when configuring Blog.Repo is deprecated, please use :pool_size instead
** (exit) exited in: GenServer.call(#PID<0.139.0>, {:query, "CREATE TABLE IF NOT EXISTS \"schema_migrations\" (\"version\" bigint …Run Code Online (Sandbox Code Playgroud) 起初我选择用来put_session存储用户ID,因为会话哈希不能被篡改.然而,似乎会话cookie仅在浏览器会话期间持续存在.当用户重新打开浏览器时,它已经消失,用户必须再次登录.
我读到另一种选择可能是为每个用户生成一个安全的随机令牌,并将其存储在数据库中,并将其放入具有高到期日期的常规cookie中.但是,鉴于这个cookie没有篡改保护AFAIK(但我可能错了)并且连接并不总是https,我猜任何人在用户和服务器之间的中间监听http都能够劫持用户会话.
因此,问题是如何以安全的方式在会话中持久保存用户ID?或者其他方式是什么?
我知道Rails有一个非常可靠的约定,有多个控制器名称ex:CustomersController.Elixir似乎并不在乎,因为路由的完成方式不同.是否有一种惯用的方式来命名控制器在凤凰城?
我正在使用使用Phoenix Framework(用Elixir编写)开发的Web应用程序.
我有一个表单字段,目前看起来像这样:
<div class="form-group">
<%= select f, :category_id, @categories, class: "form-control" %>
<%= error_tag f, :category_id %>
</div>
Run Code Online (Sandbox Code Playgroud)
这允许用户从下拉列表中选择一个类别(这没关系); 但是,我希望用户看到的是一个标准文本字段,当他们开始输入时,它会自动填充输入数据库的字符串.
与我们在Stack Overflow上发布问题时使用的Tags字段非常相似的功能.
使用Phoenix应用程序执行此操作的最佳方法是什么?我尝试过使用jQuery Autocomplete; 但是,我想要一个更"轻量级"的解决方案(不需要jQuery).
任何想法都非常感谢.感谢您的时间.
我在尝试创建数据库时遇到错误.我安装了postgres,我已经成功完成了一些测试项目.而且,我没有看到这个错误.任何帮助都会很棒:
错误:
~/Desktop/elixir/restore $ mix ecto.create
** (Mix) The database for Restore.Repo couldn't be created: tcp connect: connection refused - :econnrefused
21:52:23.978 [error] GenServer #PID<0.150.0> terminating
** (Postgrex.Error) tcp connect: connection refused - :econnrefused
(db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
(connection) lib/connection.ex:623: Connection.enter_connect/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
Run Code Online (Sandbox Code Playgroud) 如何在我计划与Ecto一起运行的查询中使用递归CTE的结果?例如,假设我有一个表,节点,结构如下:
-- nodes table example --
id parent_id
1 NULL
2 1
3 1
4 1
5 2
6 2
7 3
8 5
Run Code Online (Sandbox Code Playgroud)
我还有另一个表nodes_users结构如下:
-- nodes_users table example --
node_id user_id
1 1
2 2
3 3
5 4
Run Code Online (Sandbox Code Playgroud)
现在,我想抓住具有特定节点或其上方节点的所有用户,为了示例,我们选择具有id 8的节点.
我可以使用以下递归postgresql查询来执行此操作:
WITH RECURSIVE nodes_tree AS (
SELECT *
FROM nodes
WHERE nodes.id = 8
UNION ALL
SELECT n.*
FROM nodes n
INNER JOIN nodes_tree nt ON nt.parent_id = n.id
)
SELECT u.* FROM users u
INNER …Run Code Online (Sandbox Code Playgroud) 我需要获取客户端的IP地址,这不起作用:
def create(conn) do
ip_address = conn.inet.ip_address
# ....
Run Code Online (Sandbox Code Playgroud)
由于key :inet not found in: %Plug.Conn.我怎样才能获得IP地址呢?
我有一个模型和变更集方法.我想要一个可选字段,如果它没有传递给chargeset,我想设置一个默认值.我怎样才能做到这一点?
我有两个型号,Person并且Pet,我希望Person能够有很多宠物,但Pet到属于只有一个人:
defmodule MyApp.Person do
use MyApp.Web, :model
alias MyApp.Pet
schema "persons" do
field :name, :string
has_many :pets, Pet
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [])
|> validate_required([])
end
end
Run Code Online (Sandbox Code Playgroud)
和
defmodule MyApp.Pet do
use MyApp.Web, :model
alias MyApp.Person
schema "pets" do
field :name, :string
belongs_to :person, Person
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [])
|> validate_required([]) …Run Code Online (Sandbox Code Playgroud) 我们正在尝试将我们的Phoenix Web App部署到Linux VM(在我们的Azure案例中)使用edeliver
我们已经使用Azure部署了一个"基本"博客应用程序,edeliver并记录了以下步骤:https:
//github.com/dwyl/learn-微软蔚蓝#部分2 -部署-您的应用程序
而当我们手动运行(" 实 ")的应用程序:https://github.com/nelsonic/healthlocker
使用上的实例MIX_ENV=prod mix phoenix.server运行良好!
当我们使用以下命令将应用程序部署并运行到Azure实例时:
mix edeliver build release --verbose
mix edeliver deploy release to production
mix edeliver start production
Run Code Online (Sandbox Code Playgroud)
我们收到一条肯定的确认消息:
EDELIVER HEALTHLOCKER WITH START COMMAND
-----> starting production servers
production node:
user : root
host : 51.140.86.5
path : /home/hladmin
response: ok
START DONE!
Run Code Online (Sandbox Code Playgroud)
但是当我们netstat -plnt按照check-running-services-on-linux的 …