phoenix-framework

Ecto or Elixir datatype that maps to MySql BIGINT

天大地大妈咪最大 提交于 2020-03-22 06:55:21
问题 I'm new to Elixir and Phoenix (6 months learning), I have a situation where I want to assign an Ecto model's field (not primary key or table ID) to BIGINT in MySql. I realize when you create an Ecto model, the ID of that model in MySql table would automatically mapped to BIGINT after migration. After checking this site, I tried to create an Ecto model's field to :integer or :id in both model and its corresponding migration script but it always gives me INT datatype in MySql. Anybody knows

How to enforce JSON encoding for Phoenix Request?

孤者浪人 提交于 2020-02-27 23:42:27
问题 There's some API made with Phoenix, the API works with JSON. But, when you test it and sent JSON with curl it fail because Phoenix doesn't parse the request as JSON. You need to explicitly add application/json header to curl . I'd like to make it more robust and tell Phoenix to always parse all requests as JSON. Is there a way to force Phoenix to always treat requests as JSON and parse it as JSON? UPDATE I tried to use plug to set request headers as @AbM suggested, with the following code in

When accessing front-end SPA-links from browser, back-end fires finding no route

∥☆過路亽.° 提交于 2020-02-05 07:40:12
问题 I have a working front-end single-page-application written in JS (ReactJS), and a working back-end in Phoenix (Elixir). Everything works out fine as long as navigation happens within the application. However, when I try to access a page in the SPA from the browser, I get a route error fired from Phoenix. For example: no route found for GET /search (PhoenixApp.Router) is what I get when I access http://localhost:4000/search from the browser. When I access http://localhost:4000/search from the

Why I got #Ecto.Association.NotLoaded?

假如想象 提交于 2020-01-31 07:12:22
问题 I have teams and each team has users, so there is a join table to link users to teams as its a many to many relation, here is my models: defmodule App.Team do use App.Web, :model schema "teams" do field :owner_id, :integer has_many :team_users, {"team_user", App.TeamUser} end end defmodule App.User do use App.Web, :model schema "users" do # field :email, :string has_many :team_user, App.TeamUser end end And here is the join model: defmodule App.TeamUser do use App.Web, :model @primary_key

Undefined [controller]_path for route with two params

≡放荡痞女 提交于 2020-01-30 05:01:11
问题 I created a controller in my Phoenix application called ProgressController . This is what my router file look like: defmodule MyTestApp.Router do use MyTestApp.Web, :router pipeline :api do plug :accepts, ["json"] end scope "/", MyTestApp do pipe_through :api get "/users/:user_id/courses/:course_id", ProgressController, :show end end When I run mix phoenix.routes it outputs: progress_path GET /users/:user_id/courses/:course_id MyTestApp.ProgressController :show And I have the following test,

after_validation equivalent to create ancestors

[亡魂溺海] 提交于 2020-01-25 10:14:14
问题 The application stores locations which can belong_to one parent can have multiple ancestors During the create_location I want to add just the parent_id . The ancestors should be created automatically (including a position field to store the hierarchy level). The tree is not endless. We are talking about 4 levels. Example: USA <- California <- San Francisco <- Mission St Setup mix phx.new example cd example mix ecto.create mix phx.gen.html Maps Location locations name parent_id:references

after_validation equivalent to create ancestors

六月ゝ 毕业季﹏ 提交于 2020-01-25 10:14:13
问题 The application stores locations which can belong_to one parent can have multiple ancestors During the create_location I want to add just the parent_id . The ancestors should be created automatically (including a position field to store the hierarchy level). The tree is not endless. We are talking about 4 levels. Example: USA <- California <- San Francisco <- Mission St Setup mix phx.new example cd example mix ecto.create mix phx.gen.html Maps Location locations name parent_id:references

How to selectively disable CSRF check in Phoenix framework

我的梦境 提交于 2020-01-22 13:19:30
问题 I'm trying to create a Facebook Page Tab which points to my website. Facebook sends a HTTP POST request to the url of my website. The problem here is that the server has a built-in CSRF check, and it returns the following error: (Plug.CSRFProtection.InvalidCSRFTokenError) invalid CSRF (Cross Site Forgery Protection) token, make sure all requests include a '_csrf_token' param or an 'x-csrf-token' header` The server expects a CSRF token that Facebook can't have. So, I want to selectively

How to run updating in migration for Ecto?

落爺英雄遲暮 提交于 2020-01-21 03:11:06
问题 I use Phoenix and Ecto in one of my project. I want to add a column to one table, and I expect it to be a NOT NULL column. But I already have some existed data, so I decide to add the column, update all rows to some value and modify the column to NOT NULL . I tried these two code: # solution 1 def up do alter table(:channels) do add :type, :integer Exchat.Repo.update_all("channels", set: [type: 1]) modify :type, :integer, null: false end end # solution 2 def up do alter table(:channels) do

Elixir/Phoenix binary_to_atom

白昼怎懂夜的黑 提交于 2020-01-20 08:55:26
问题 I have a form <%= select f, :user_id, ["刺繡等等我": "2", "wow": "3"] %> If I use only english language, it works perfectly. But chinese, or any other returns error ** (ArgumentError) argument error :erlang.binary_to_atom("刺繡等等我", :utf8) (elixir) src/elixir_parser.yrl:512: :elixir_parser.yeccpars2_93/7 I believe it has do to with the encoding. How can I convert the string to the acceptable format? Thanks in advance! 回答1: Atoms cannot contain codepoints above 255 as of the current version of Erlang