phoenix-framework

Phoenix and Ecto and SELECTs

核能气质少年 提交于 2020-01-14 14:39:28
问题 I have an association set up in Ecto models in Phoenix. An Organization has many OrganizationMembers. In the OrganizationMember controller's Edit method, I'm trying to create a SELECT element that will hold all of the Organizations to choose from. In the edit definitiion, I've got the following two lines: # organizations = Enum.to_list(from(o in Organization, order_by: o.name, select: [o.name, o.id])) organizations = from(o in Organization, order_by: o.name, select: {o.name, o.id}) This is my

How to render html template in javascript template in Phoenix Framework

非 Y 不嫁゛ 提交于 2020-01-13 19:25:06
问题 Let's say I have 2 files, create.js.eex and post.html.eex and I want to render the contents of the post.html.eex template inside the create.js.eex template. Something like this: $("#something").append("<%= safe_to_string render "post.html", post: @post %>"); The example above doesn't work because I need to escape quotes and other things in the string that gets returned and I can't find a way to do it 回答1: Use escape_javascript: $("#something").append("<%= escape_javascript render("post.html",

What is the different between web/static and priv/static in phoenix?

北城余情 提交于 2020-01-13 11:29:13
问题 I am new to elixir and phoenix. Now I have trouble with the static assets in phoenix. I want to add a js file in my page, and I add the following code in my template: <script src="<%= static_path(@conn, "/js/test.js") %>"></script> and then create a js file at web/static/js/test.js . However, I got the error about test.js is not found in the browser's console. I notice there is a priv/static/js folder, and I try to create the js file at priv/static/js/test.js . This time, the error gone. I am

How to add a custom error message for a required field in Phoenix framework

家住魔仙堡 提交于 2020-01-13 10:12:49
问题 How can I change the error message for required fields? If I have something like that @required_fields ~w(name email) and I want to show "no way it's empty" instead of the default value of "can't be blank" ? 回答1: The "can't be blank" error message is hardcoded into Ecto at the moment. However, you can replace this error message by doing: def changeset(model, params \\ :empty) do model |> cast(params, @required_fields, @optional_fields) |> required_error_messages("no way it's empty") end def

How to add a custom error message for a required field in Phoenix framework

你说的曾经没有我的故事 提交于 2020-01-13 10:12:09
问题 How can I change the error message for required fields? If I have something like that @required_fields ~w(name email) and I want to show "no way it's empty" instead of the default value of "can't be blank" ? 回答1: The "can't be blank" error message is hardcoded into Ecto at the moment. However, you can replace this error message by doing: def changeset(model, params \\ :empty) do model |> cast(params, @required_fields, @optional_fields) |> required_error_messages("no way it's empty") end def

Raw SQL with Ecto

安稳与你 提交于 2020-01-13 08:16:51
问题 I'm very new in the world of Elixir and Phoenix Framework. I'm trying to follow TheFireHoseProject tutorial, but having problems with querying raw SQL with Ecto. The tutorial says this should work: defmodule Queries do def random do query = Ecto.Adapters.Postgres.query( Repo, "SELECT id, saying, author from quotes ORDER BY RANDOM() LIMIT 1", []) %Postgrex.Result{rows: [row]} = query {id, saying, author} = row %Splurty.Quote{id: id, saying: saying, author: author} end end I'm getting a runtime

Raw SQL with Ecto

旧街凉风 提交于 2020-01-13 08:16:26
问题 I'm very new in the world of Elixir and Phoenix Framework. I'm trying to follow TheFireHoseProject tutorial, but having problems with querying raw SQL with Ecto. The tutorial says this should work: defmodule Queries do def random do query = Ecto.Adapters.Postgres.query( Repo, "SELECT id, saying, author from quotes ORDER BY RANDOM() LIMIT 1", []) %Postgrex.Result{rows: [row]} = query {id, saying, author} = row %Splurty.Quote{id: id, saying: saying, author: author} end end I'm getting a runtime

Route to a static page in phoenix-framework

最后都变了- 提交于 2020-01-12 07:07:09
问题 I want to run an angularJS front end with a phoenix backend for my site. I would like my root route to direct the user to a pre-built page in the static directory which contains my angular client and then use phoenix to run the API. I have done this in the past with ruby on rails by route matching like this: get '/', to: redirect('/foobar.html') Is there a way to do something similar with phoenix? 回答1: Not right now. You need to create a controller and then in the controller: defmodule MyApp

How can a Phoenix application tailored only to use channels scale on multiple machines? Using HAProxy? How to broadcast messages to all nodes?

只谈情不闲聊 提交于 2020-01-12 05:24:06
问题 I use the node application purely for socket.io channels with Redis PubSub, and at the moment I have it spread across 3 machines, backed by nginx load balancing on one of the machines. I want to replace this node application with a Phoenix application, and I'm still all new to the erlang/Elixir world so I still haven't figured out how a single Phoenix application can span on more than one machine. Googling all possible scaling and load balancing terms yielded nothing. The 1.0 release notes

How can a Phoenix application tailored only to use channels scale on multiple machines? Using HAProxy? How to broadcast messages to all nodes?

让人想犯罪 __ 提交于 2020-01-12 05:23:10
问题 I use the node application purely for socket.io channels with Redis PubSub, and at the moment I have it spread across 3 machines, backed by nginx load balancing on one of the machines. I want to replace this node application with a Phoenix application, and I'm still all new to the erlang/Elixir world so I still haven't figured out how a single Phoenix application can span on more than one machine. Googling all possible scaling and load balancing terms yielded nothing. The 1.0 release notes