我的应用程序适用于 api json 请求和常规 html。我的路由器.ex
defmodule MyApp.Router do
use MyApp.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/api", MyApp do
pipe_through :api # Use the default browser stack
scope "/v1", V1, as: :v1 do
resources "/users", UserController, except: [:new, :edit, :index]
end
end
scope "/", MyApp do
pipe_through :browser # Use the default browser stack
get "/confirm/:token", UserController, :confirm, as: :user_confirm
end
end …Run Code Online (Sandbox Code Playgroud) 在我的ecto模型中,我需要在字段中指定:user_timestamp从unix_time转换的时间。移民:
def change do
alter table(:operations) do
add :user_timestamp, :naive_datetime, null: false
end
end
Run Code Online (Sandbox Code Playgroud)
我的operation.ex
schema "operations" do
field :sum, :float
field :name, :string
field :user_timestamp, :naive_datetime
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name, :sum, :user_timestamp])
|> validate_required([:sum, :user_timestamp])
|> convert_unix_time_to_ecto
end
defp convert_unix_time_to_ecto(changeset) do
put_change(changeset, :user_timestamp, Ecto.DateTime.from_unix!(changeset.changes.user_timestamp, :seconds) |> Ecto.DateTime.to_naive())
end
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试添加具有user_timestamp的要求时,出现错误:
[error] #PID<0.492.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /api/v1/operations
** (exit) an exception was raised:
** (FunctionClauseError) no function clause …Run Code Online (Sandbox Code Playgroud) 在Phoenix应用程序中,我有一个函数可以获取两个地图,并通过Ecto.Changeset在数据库中创建两个条目。
def create_user_with_data(user_attrs, data_attrs) do
name = cond do
data_attrs["name"] ->
data_attrs["name"]
data_attrs[:name] ->
data_attrs[:name]
true -> nil
end
Ecto.Multi.new()
|> Ecto.Multi.insert(:user, User.registration_changeset(%User{}, Map.put(user_attrs, :name, name)))
|> Ecto.Multi.run(:user_data, fn(%{user: user}) ->
%MyApp.Account.UserData{}
|> MyApp.Account.UserData.changeset(Map.put(data_attrs, :user_id, user.id))
|> Repo.insert()
end)
|> Repo.transaction()
end
Run Code Online (Sandbox Code Playgroud)
因为这些图中的键既可以是原子也可以是线,所以我必须检查这些键。
但是表达
Map.put(user_attrs, :name, name)
Run Code Online (Sandbox Code Playgroud)
会导致错误
** (Ecto.CastError) expected params to be a map with atoms or string keys, got a map with mixed keys: %{:name => "John", "email" => "m@gmail.com"}
Run Code Online (Sandbox Code Playgroud)
如果键是字符串。
处理此问题是否有最佳实践?
我已经安装了 mysql workbench v. 8.0.13,并且正在尝试运行迁移工具,但出现错误:
Could not import the pyodbc python module. You need pyodbc 2.1.8 or newer for migrations from RDBMSes other than MySQL.
Run Code Online (Sandbox Code Playgroud)
我已经安装了pyodbc:
Python 3.7.0 (default, Sep 28 2018, 16:44:32)
[GCC 8.2.1 20180831] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyodbc
>>> pyodbc.version
'4.0.24'
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
OS: Linux my-pc 4.19.0-3-MANJARO #1 SMP PREEMPT Sat Oct 27 22:40:22 UTC 2018 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud) 有两种模型:资源和元数据:
defmodule Myapp.Repo.Migrations.CreateResources do
use Ecto.Migration
def change do
create table(:resources) do
add :name, :string
add :parent_id, references(:resources, on_delete: :delete_all)
timestamps
end
create index(:resources, [:parent_id])
end
end
defmodule Myapp.Repo.Migrations.CreateMetadata do
use Ecto.Migration
def change do
create table(:metadata) do
add :size, :integer
add :resource_id, references(:resources)
timestamps
end
create index(:metadata, [:resource_id])
end
end
schema "resources" do
field :name, :string
belongs_to :parent, Myapp.Resource
has_one :metadata, Myapp.Metadata, on_delete: :delete_all
has_many :childs, Myapp.Resource, foreign_key: :parent_id, on_delete: :delete_all
timestamps()
end
schema "metadata" do
field :size, :integer …Run Code Online (Sandbox Code Playgroud) 我有一个vuetify组件来显示标签列表.
<template>
<div>
<v-chip
v-for="tag in tags"
:key="tag.id"
v-model="???"
@input="onClose(tag)"
close
>
{{tag.name}}
</v-chip>
</div>
</template>
<script>
export default {
name: 'TagIndex',
props: ['tags'],
methods: {
onClose(tag){
console.log('close tag')
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
vuetify文档说:
可以使用v模型控制可闭合的芯片.
如果标签列表是动态的,我不明白我需要指定哪种对象作为每个标签的模型.
我试图根据标签列表创建一个数组:
data(){
return {
clonedTags: this.tags.map((t) => {return true})
}
}
Run Code Online (Sandbox Code Playgroud)
但是失败了
在我的凤凰控制器中,我使用:
...
def delete(conn, %{"id" => id}) do
post = Repo.get!(Post, id)
with {:ok, %{post: %Post{}}} <- Posts.delete_post(post) do
send_resp(conn, :no_content, "")
end
end
...
Run Code Online (Sandbox Code Playgroud)
在我的测试中:
...
test "deletes chosen post", %{conn: orig_conn, user: user, post: post} do
conn = delete orig_conn, v1_post_path(orig_conn, :delete, post)
assert response(conn, 204)
conn = delete orig_conn, v1_post_path(orig_conn, :delete, post)
assert response(conn, 404)
end
...
Run Code Online (Sandbox Code Playgroud)
但是,如果帖子不存在,则此测试将失败:
...
** (Ecto.NoResultsError) expected at least one result but got none in query:
...
Run Code Online (Sandbox Code Playgroud)
为什么?我看到这个,Ecto.NoResultsError应该返回404状态
Phoenix版本1.3,phoenix-ecto 3.3,ecto …