phoenix-framework

How to import users from csv file with elixir/phoenix?

送分小仙女□ 提交于 2020-01-06 17:58:52
问题 I have a csv file with a list of users and the following import method inside UserController and I would like to import these users by submitting the csv file with a form. And It looks like I'm doing it wrong. users_controller def import(conn, %{"user" => user_params}) do user_params["file"] |> File.stream!() |> CSV.decode |> Enum.each(fn(user) -> User.changeset(%User{}, %{name: Enum.at(user, 0), email: Enum.at(user, 1)}) |> Repo.insert end) conn |> put_flash(:info, "Imported") |> redirect(to

Phoenix CSRF token not matching

怎甘沉沦 提交于 2020-01-05 05:26:14
问题 Trying to get ajax working the Phoenix. I get the csrf token by doing the following so i have it: <input type="hidden" id="_csrf_token" name="_csrf_token" value="<%= get_csrf_token() %>"> Then use it like so: $.ajax({ type: "POST", url: "<%= lesson_path @conn, :create %>", beforeSend: function(xhr) { token = $('#_csrf_token').val(); xhr.setRequestHeader('_csrf_token', token ); }, data: data, success: function(data, textStatus, jqXHR) { alert(textStatus); } }); The issue is that the token i

Make Ecto queries more efficient

本秂侑毒 提交于 2020-01-04 14:05:26
问题 I'm trying to see if my current user's teams overlap with the passed in user's teams. I have something that works but I'm curious if it could me more efficient. Here is what I have: user_teams = from( t in MyApp.Team, left_join: a in assoc(t, :accounts), where: p.owner_id == ^user.id or (a.user_id == ^user.id and t.id == a.project_id) ) |> Repo.all current_user_teams = from( t in MyApp.Team, left_join: a in assoc(t, :accounts), where: t.owner_id == ^current_user.id or (a.user_id == ^current

function is undefined or private while should be accessible

我怕爱的太早我们不能终老 提交于 2020-01-04 07:48:20
问题 I'm trying to reach a method from my Accounts domain. I'm able to use any method from the Accounts domain in iex except for the mark_as_admin/1 (the last one). here is the domain defmodule Storex.Accounts do import Ecto.Query, warn: false alias Storex.Repo alias Storex.Accounts.User def create_user(attrs \\ %{}) do %User{} |> User.changeset(attrs) |> Repo.insert() end def get_user!(id) do Repo.get!(User, id) end def new_user() do User.changeset(%User{}, %{}) end def authenticate_user(email,

Docker container is killed after ~1 minute

家住魔仙堡 提交于 2020-01-04 01:58:14
问题 I made a very small Phoenix Framework app (only slightly modified from what you get when you run: mix phoenix.new). I've been trying to deploy it in a Docker container. It works fine while the container is running, but it always dies within a minute of startup with a message of "Killed." Whether I make requests to it or not does not seem to matter. I tried watching the docker events, and got the following: $ docker events 2016-04-09T16:24:02.538602484-04:00 container create

Best practice to pass data from Phoenix to Javascript

此生再无相见时 提交于 2020-01-03 17:32:09
问题 I see 3 ways of doing this. Using <%= %> inside <script> in *.html.eex Use channels to pass data to Javascript Build a json api #1 seems the easiest but I couldn't find or think of a good way to do it yet. Note: real-time update is not my requirement. 回答1: I would never use a <script></script> for that, in my projects I have this pattern: <!-- Layout --> <div id="config" data-environment="..." ></div> I always provide the current environment in the master layout, I have a config.js file with

Best practice to pass data from Phoenix to Javascript

被刻印的时光 ゝ 提交于 2020-01-03 17:32:06
问题 I see 3 ways of doing this. Using <%= %> inside <script> in *.html.eex Use channels to pass data to Javascript Build a json api #1 seems the easiest but I couldn't find or think of a good way to do it yet. Note: real-time update is not my requirement. 回答1: I would never use a <script></script> for that, in my projects I have this pattern: <!-- Layout --> <div id="config" data-environment="..." ></div> I always provide the current environment in the master layout, I have a config.js file with

How can I add days to Ecto.DateTime?

人走茶凉 提交于 2020-01-03 10:45:47
问题 I have a date-time which I create like this: Ecto.DateTime.from_erl({{2015, 3, 10}, {0, 0, 0}}) It's a Phoenix app. I want to add days to it with no any additional third-party library. How? 回答1: You can use erlang's :calendar module to manipulate dates without additional dependencies. A standard way of adding days would be to use :calendar.date_to_gregorian_days/1 do the addition and convert back to the tuple format with :calendar.gregorian_days_to_date/1 . 回答2: Proper elixir syntax weekday=

Ecto has_many :through in form

非 Y 不嫁゛ 提交于 2020-01-02 03:33:45
问题 I am trying to get a has_many :through relationship working in Ecto for a many-many relationship between a User model and Group model. The only information I was able to find online was related to nested attributes in a post by José Valim here (which is excellent, by the way). Since the groups already exist in the system, I was hoping to do a multiple select input. I ran into a couple issues in doing that. I don't believe that it is possible to assign the groups association in the changeset

UndefinedFunctionError phoenix elixir new project

流过昼夜 提交于 2020-01-02 00:59:09
问题 With a fresh install of Phoenix, I tried to trigger a new project, and when I try to run the server, I'm met with: Unchecked dependencies for environment dev: * gettext (Hex package) the dependency is not available, run "mix deps.get" * phoenix_live_reload (Hex package) the dependency is not available, run "mix deps.get" * cowboy (Hex package) * List item ... but when I run mix deps.get I get: Running dependency resolution ** (UndefinedFunctionError) undefined function Access.Map.get_and