Phoenix 文档说“Phoenix 模板是预编译的这一事实使它们非常快。” .
我有几个问题,预编译模板实际上意味着什么,与 Rails App 之类的东西相比,这在生产中会产生多大的显着差异。
我用它来为用户创建一个会话:
put_session(conn, :user_id, user_id)
Run Code Online (Sandbox Code Playgroud)
但它很快就会过期,我想大约在一天内。我希望它在 2 周左右。如何指定有效的时间范围?
我在 Phoenix 应用程序中有以下模块。此代码基本上是共享规范示例。它看起来像这样:
defmodule TattooBackend.CurrentPasswordRequiredTest do
@moduledoc """
This module defines tests that are testing api current password requirement.
"""
import Phoenix.ConnTest
alias TattooBackend.Web.Router
defmacro test_current_password_required_for(method, path_name, action) do
quote do
test "without current password", %{conn: conn} do
method = unquote(method)
path_name = unquote(path_name)
action = unquote(action)
account = insert(:account)
assert make_unauthenticated_request(conn, @endpoint, method, path_name, action, account) == %{
"error" => "B??dne has?o."
}
end
end
end
def make_unauthenticated_request(conn, endpoint, method, path_name, action, account) do
path = apply(Router.Helpers, path_name, [conn, …Run Code Online (Sandbox Code Playgroud) 在 ruby on rails(active record) 中,我们可以通过调用update_counter方法来更新计数器
。例如在 Rails 中,您只需要编写:
# For the Post with id of 5, decrement the comment_count by 1, and
# increment the action_count by 1
Post.update_counters 5, :comment_count => -1, :action_count => 1
# Executes the following SQL:
# UPDATE posts
# SET comment_count = comment_count - 1,
# action_count = action_count + 1
# WHERE id = 5
Run Code Online (Sandbox Code Playgroud)
考虑到我必须更新多列的计数器,有没有什么简单的方法可以在 elixir/phoenix 中做到这一点?
我有这个功能:我正在尝试与记录列表进行比较,以查看两者之间是否匹配。
def current_user_has_team?(user, teams) do
user = user |> Repo.preload(:teams)
Enum.member?(user.teams, teams)
end
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为当只有一条记录并且它们匹配时它返回 false。
我怎么说:“看看这个记录列表,这些记录是否与另一个列表匹配?” 在长生不老药?
在 Ruby 中是这样的:
list_1 = [1,2,3]
list_2 = [3,4,5]
(list_1 & list_2).any? => true
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个按钮,使给定用户成为管理员。为此,我想为请求创建一个路由post /users/:id/admin。为了做到这一点,我正在尝试创建一个嵌套资源,如下所示:
resources "/users", UserController, only: [:new, :create, :index] do
resources "/admin", UserController, only: [:post]
end
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时mix phx.routes | grep users,我只能得到这些路线:
user_path GET /users StorexWeb.UserController :index
user_path GET /users/new StorexWeb.UserController :new
user_path POST /users StorexWeb.UserController :create
Run Code Online (Sandbox Code Playgroud)
好像未声明嵌套资源一样。我的资源声明有什么问题?我该如何解决?
我的混合文件包含
{:guardian, "~> 1.0"},
{:guardian_db, "~> 1.1"},
Run Code Online (Sandbox Code Playgroud)
和配置包含
config :my_app, MyApp.Guardian,
issuer: "my_app",
ttl: {30, :days},
allowed_drift: 2000,
verify_issuer: true,
# mix guardian.gen.secret (to get a key for dev and prod envs)
secret_key: "yKwVGXFyH6nbiE+ELRMLYjCDC3QughF02LN+xPlB7z2loDKeNuBJ6RIUdTMBul23"
config :guardian, Guardian.DB,
repo: Qserv.BaseRepo,
schema_name: "sessions", # default
token_types: ["refresh_token"], # store all token types if not set
sweep_interval: 60
Run Code Online (Sandbox Code Playgroud)
我的应用程序有这条线
worker(Guardian.DB.Token.SweeperServer, []),
Run Code Online (Sandbox Code Playgroud)
和我的会话表迁移
defmodule MyApp.Repo.Migrations.CreateTable.Auth.Sessions do
use Ecto.Migration
@table :sessions
def change do
create table(@table, primary_key: false) do
add :jti, :string, primary_key: …Run Code Online (Sandbox Code Playgroud) 我想出了一种使用arc和arc_ecto在本地上传附件的方法。但是,我想找到一种方法,让我的应用程序的用户只需单击浏览器上的下载按钮即可下载或保存这些上传的文件。下载的文件将位于单独的文件夹中。
我已经浏览了很多来源,但我似乎还没有得到它。
请问我该怎么做?
我想知道如何自动填充时间戳()生成的“updated_at”和“inserted_at”列。
目标是不指定这些值,因为它必须是自动的。我知道如何处理其他列。
schema "users" do
field :newsletter, :boolean, default: false
timestamps(type: :utc_datetime)
end
Run Code Online (Sandbox Code Playgroud)
我试过了
schema "users" do
field :newsletter, :boolean, default: false
field :inserted_at,:utc_datetime, default: :utc_datetime
field :updated_at,:utc_datetime, default: :utc_datetime
end
Run Code Online (Sandbox Code Playgroud)
但它似乎不起作用或运行时未考虑更改:
mix ecto.migrate
Run Code Online (Sandbox Code Playgroud)
我的代码是错误的还是我使用了错误的命令行?
在我的 SQL 模式中,我有
newsletter BOOLEAN DEFAULT FALSE NOT NULL,
inserted_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
Run Code Online (Sandbox Code Playgroud)
不管我尝试什么,最后两行都不能有“DEFAULT TIMESTAMP”。
我必须先放下桌子才能工作吗?
我正在使用 phoenix 创建服务器并使用编辑器 VSCode。
当我启动 server:mix phx.server并且我更改了代码时,它不会重新编译,我必须关闭并再次运行。
应该在可以自动重新编译的地方设置扩展或配置吗?
文件dev.exs
config :jwtuser, Jwtuser.Endpoint,
http: [port: 5000],
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
cd: Path.expand("../assets", __DIR__)]]
Run Code Online (Sandbox Code Playgroud)
在mix.exs 中
def project do
[
app: :jwtuser,
version: "0.0.1",
elixir: "~> 1.4",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix, :gettext] ++ Mix.compilers,
start_permanent: Mix.env == :prod,
aliases: aliases(),
deps: deps(),
erlc_options: erlc_options()
]
end
Run Code Online (Sandbox Code Playgroud)