我想知道目前使用elixir/phoenix框架的网址,我怎么能得到这个?
编辑#1:
我的nginx配置文件:
server {
client_max_body_size 100M;
listen 80;
server_name *.babysittingbordeaux.dev *.babysittingparis.dev
access_log /usr/local/var/log/nginx/baby-access.log;
error_log /usr/local/var/log/nginx/baby-error.log;
location / {
proxy_pass http://127.0.0.1:4000;
}
}
Run Code Online (Sandbox Code Playgroud)
码:
Atom.to_string(conn.scheme) <> "://" <> (Enum.into(conn.req_headers, %{}) |> Map.get("host")) <> conn.request_path
Run Code Online (Sandbox Code Playgroud)
那个例子返回http://127.0.0.1:4000/,我想得到http://www.babysittingbordeaux.dev/
我正处于开发模式.
网络/模型/ post.ex
defmodule Baby.Post do
use Baby.Web, :model
schema "posts" do
field :cover, :string
field :email, :string
field :firstname, :string
field :lastname, :string
field :birthday_day, :integer
field :birthday_month, :integer
field :birthday_year, :integer
field :description, :string
field :phone, :string
timestamps
end
@required_fields ~w(email firstname lastname birthday_day birthday_month birthday_year description phone)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params …Run Code Online (Sandbox Code Playgroud) 我想在我的ecto模型中添加自定义验证规则.
假设我有这段代码:
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> validate_length(:description, min: 280)
|> my_awesome_validation(:email)
end
def my_awesome_validation(email) do
# ??
end
Run Code Online (Sandbox Code Playgroud)
我应该在my_awesome_validation函数中写什么来抛出错误等等?
我有一个Elixir/Phoenix应用程序,根据域名(也称为租户)做出不同反应.
租户具有特定的区域设置,例如"fr_FR","en_US"等.
我想根据当前的语言环境翻译路由器的URI :
# EN
get "/classifieds/new", ClassifiedController, :new
# FR
get "/annonces/ajout", ClassifiedController, :new
Run Code Online (Sandbox Code Playgroud)
到目前为止,我认为可以做类似的事情(伪代码):
if locale() == :fr do
scope "/", Awesome.App, as: :app do
pipe_through :browser # Use the default browser stack
get "/annonces/ajout", ClassifiedController, :new
end
else
scope "/", Awesome.App, as: :app do
pipe_through :browser # Use the default browser stack
get "/classifieds/new", ClassifiedController, :new
end
end
Run Code Online (Sandbox Code Playgroud)
由于路由器是在服务器启动期间编译的,因此它不起作用,因此您没有当前连接的上下文(区域设置,域,主机等).
到目前为止,我的解决方案(可行)是创建两个带有两个别名的作用域:
scope "/", Awesome.App, as: :fr_app do
pipe_through :browser # Use the default browser stack …Run Code Online (Sandbox Code Playgroud) 在我的Ember应用程序启动之前,我想根据URL动态设置变量:
// Dummy example
if (window.location.hostname == 'awesomewebsite.com') {
// Set a "global variable" called town
}
Run Code Online (Sandbox Code Playgroud)
我希望有可能依赖该变量来做一些事情(在组件,模板等中).
最好的方法是什么?