moai-lang-elixir by modu-ai/moai-adk
npx skills add https://github.com/modu-ai/moai-adk --skill moai-lang-elixirElixir 1.17+ 开发专家 - Phoenix 1.7、LiveView、Ecto、OTP 模式及函数式编程。
自动触发:.ex、.exs 文件,mix.exs,config/ 目录,Phoenix/LiveView 相关讨论
核心能力:
Phoenix 控制器:使用 MyAppWeb 定义模块并指定 :controller。为上下文模块创建别名,例如 MyApp.Accounts。定义动作函数,如 show,接收 和包含解构 的参数映射。使用带感叹号的上下文函数(如 )获取数据,并通过 将数据传递给模板进行渲染。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
connidget_user!assigns对于创建动作,对上下文结果元组进行模式匹配。对于 :ok 元组,使用管道操作符 put_flash 添加成功消息,并使用 ~p 符号进行重定向以使用已验证路由。对于包含 Ecto.Changeset 的错误元组,渲染表单模板并传递变更集。
带变更集的 Ecto 模式:使用 Ecto.Schema 定义模块并导入 Ecto.Changeset。定义包含字段声明的模式块,字段类型如 :string,以及虚拟字段。创建一个接收结构和属性的变更集函数,使用管道操作符链式调用 cast(包含要转换的字段列表)、validate_required、带正则表达式的 validate_format、带 min 选项的 validate_length 以及 unique_constraint。
GenServer 模式:使用 GenServer 定义模块。创建 start_link 函数,接收初始值并调用 GenServer.start_link,参数为 __MODULE__、初始值和 name 选项。定义客户端 API 函数,调用 GenServer.call 并传递 __MODULE__ 和消息原子。实现 init 回调,返回包含状态的 :ok 元组。为每个消息实现 handle_call 回调,返回包含响应和新状态的回复元组。
高级模式匹配:在函数头部根据映射键和类型进行模式匹配。使用带 when 的卫语句添加约束,如 is_binary 或 byte_size 检查。添加一个捕获所有无效输入的子句,返回错误元组。
带错误处理的管道操作符:使用 with 特殊形式来链接可能失败的操作。使用左箭头对每个步骤进行模式匹配,所有步骤成功后,返回最终的 :ok 元组。在 else 块中,处理错误元组并原样返回。
用于多态性的协议:使用 @doc 和 defprotocol 定义协议。使用带 for: 选项的 defimpl 为特定类型实现协议。每个实现为该类型提供特定的行为。
LiveView 组件:使用 MyAppWeb 定义模块并指定 :live_view。实现 mount 回调,接收参数、会话和套接字,返回包含分配状态的 :ok 元组。实现 handle_event 回调处理用户交互,使用 update 辅助函数返回包含更新后套接字的 :noreply 元组。实现 render 回调,使用 ~H 符号编写 HEEx 模板,通过 @ 前缀访问分配值。
带变更集的 LiveView 表单:在 mount 中,创建初始变更集并使用 to_form 辅助函数进行分配。实现 validate 事件处理器,创建带有 :validate 动作的变更集并重新分配表单。实现 save 事件处理器,调用上下文创建函数,成功时使用 put_flash 和 push_navigate,失败时重新分配带有错误变更集的表单。
Phoenix 频道:使用 MyAppWeb 定义模块并指定 :channel。实现 join 回调,匹配带尖括号的动态片段主题模式。使用 send 和 self() 发送 after_join 消息。使用 push 实现 handle_info 处理服务器端消息。使用 broadcast! 实现 handle_in 处理客户端消息并发送给所有订阅者。
已验证路由:在 router.ex 的 scope 块内使用 live 宏定义 LiveView 路由。在模板和控制器中使用 ~p 符号表示已验证路由,并使用插值语法处理动态片段。
用于事务的 Multi:使用 Ecto.Multi.new() 并通过 Ecto.Multi.update 和命名原子及变更集函数进行管道操作。当需要前一步骤的结果时,使用带函数回调的 Ecto.Multi.insert。将最终的 Multi 传递给 Repo.transaction(),该函数返回包含命名结果的 :ok 或 :error 元组。
查询组合:创建一个包含可组合查询函数的查询模块。定义一个返回 from 表达式的基础函数。创建带有查询默认参数的过滤函数,返回带有 where 子句的修改后的 from 表达式。在传递给 Repo.all 之前,使用管道操作符链式调用这些函数。
如需全面覆盖以下内容:
请参阅:
moai-domain-backend - REST API 和微服务架构moai-domain-database - SQL 模式和查询优化moai-workflow-testing - DDD 和测试策略moai-essentials-debug - AI 驱动的调试moai-platform-deploy - 部署和基础设施常见问题:
Elixir 版本检查:运行 elixir --version 验证是否为 1.17+ 版本,运行 mix --version 查看 Mix 构建工具版本。
依赖问题:运行 mix deps.get 获取依赖项,mix deps.compile 编译依赖项,mix clean 清除构建产物。
数据库迁移:运行 mix ecto.create 创建数据库,mix ecto.migrate 运行迁移,mix ecto.rollback 回滚最后一次迁移。
Phoenix 服务器:运行 mix phx.server 启动服务器,iex -S mix phx.server 启动带 IEx 交互式 shell 的服务器,MIX_ENV=prod mix release 构建生产发布版本。
LiveView 无法加载:
mix.exs 依赖项中列出了 Phoenix.LiveView最后更新:2026-01-11 状态:活跃 (v1.1.0)
每周安装数
99
代码仓库
GitHub 星标数
888
首次出现
Jan 22, 2026
安全审计
安装于
opencode81
codex78
gemini-cli76
claude-code76
github-copilot70
antigravity62
Elixir 1.17+ Development Specialist - Phoenix 1.7, LiveView, Ecto, OTP patterns, and functional programming.
Auto-Triggers: .ex, .exs files, mix.exs, config/, Phoenix/LiveView discussions
Core Capabilities:
Phoenix Controller: Define a module using MyAppWeb with :controller. Create alias for the context module like MyApp.Accounts. Define action functions like show taking conn and params map with destructured id. Fetch data using the context function with bang like get_user! and render the template passing the data as assigns.
For create actions, pattern match on the context result tuple. On ok tuple, use pipe operator to put_flash with success message and redirect using ~p sigil for verified routes. On error tuple with Ecto.Changeset, render the form template passing the changeset.
Ecto Schema with Changeset: Define a module using Ecto.Schema and importing Ecto.Changeset. Define the schema block with field declarations including types like :string and virtual fields. Create a changeset function taking the struct and attrs, using pipe operator to chain cast with the list of fields to cast, validate_required, validate_format with regex, validate_length with min option, and unique_constraint.
GenServer Pattern: Define a module using GenServer. Create start_link taking initial_value and calling GenServer.start_link with MODULE , initial_value, and name option. Define client API functions that call GenServer.call with MODULE and the message atom. Implement init callback returning ok tuple with state. Implement handle_call callbacks for each message, returning reply tuple with response and new state.
Pattern Matching Advanced: Define function heads with pattern matching on map keys and types. Use guard clauses with when to add constraints like is_binary or byte_size checks. Add a catch-all clause returning error tuple for invalid inputs.
Pipe Operator with Error Handling: Use the with special form for chaining operations that may fail. Pattern match each step with left arrow, and on successful completion of all steps, return the final ok tuple. In the else block, handle error tuples by returning them unchanged.
Protocols for Polymorphism: Define a protocol with @doc and function specification using defprotocol. Implement the protocol for specific types using defimpl with for: option. Each implementation provides the specific behavior for that type.
LiveView Component: Define a module using MyAppWeb with :live_view. Implement mount callback taking params, session, and socket, returning ok tuple with assigned state. Implement handle_event callback for user interactions, returning noreply tuple with updated socket using update helper. Implement render callback with assigns, using ~H sigil for HEEx templates with assigns accessed via @ prefix.
LiveView Form with Changesets: In mount, create initial changeset and assign using to_form helper. Implement validate event handler that creates changeset with :validate action and reassigns the form. Implement save event handler that calls context create function, on success using put_flash and push_navigate, on error reassigning the form with error changeset.
Phoenix Channels: Define a module using MyAppWeb with :channel. Implement join callback matching on topic pattern with angle brackets for dynamic segments. Use send with self() for after_join messages. Implement handle_info for server-side messages using push. Implement handle_in for client messages using broadcast! to send to all subscribers.
Verified Routes: Define routes in router.ex within scope blocks using live macro for LiveView routes. Use ~p sigil for verified routes in templates and controllers, with interpolation syntax for dynamic segments.
Multi for Transactions: Use Ecto.Multi.new() and pipe through operations using Ecto.Multi.update with name atoms and changeset functions. Use Ecto.Multi.insert with function callback when needing results from previous steps. Pipe final Multi to Repo.transaction() which returns ok or error tuple with named results.
Query Composition: Create a query module with composable query functions. Define a base function returning from expression. Create filter functions with default parameter for query, returning modified from expression with where clause. Chain functions with pipe operator before passing to Repo.all.
For comprehensive coverage including:
See:
moai-domain-backend - REST API and microservices architecturemoai-domain-database - SQL patterns and query optimizationmoai-workflow-testing - DDD and testing strategiesmoai-essentials-debug - AI-powered debuggingmoai-platform-deploy - Deployment and infrastructureCommon Issues:
Elixir Version Check: Run elixir --version to verify 1.17+ and mix --version for Mix build tool version.
Dependency Issues: Run mix deps.get to fetch dependencies, mix deps.compile to compile them, and mix clean to remove build artifacts.
Database Migrations: Run mix ecto.create to create database, mix ecto.migrate to run migrations, and mix ecto.rollback to rollback last migration.
Phoenix Server: Run mix phx.server to start the server, iex -S mix phx.server to start with IEx shell, and MIX_ENV=prod mix release to build production release.
LiveView Not Loading:
Last Updated: 2026-01-11 Status: Active (v1.1.0)
Weekly Installs
99
Repository
GitHub Stars
888
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
opencode81
codex78
gemini-cli76
claude-code76
github-copilot70
antigravity62
Shader Craft:36种GLSL着色器技术,实时视觉效果开发指南
555 周安装