小编She*_*yar的帖子

Rails ActiveRecord按连接表关联的数量排序

我有一个Resource可以使用"Acts As Votable"宝石(Github页面)投票的模型.投票系统运作完美,但我试图按votes每个Resource有多少页面显示页面.

目前我的控制器根据标签提取资源,而不是订购:

@resources = Resource.where(language_id: "ruby")
Run Code Online (Sandbox Code Playgroud)

如果我获取一个单独的资源并调用"@ resource.votes.size",它将返回它有多少票.但是,投票是另一个表,所以我认为需要进行某种联接,但我不知道如何做.我需要的是一个很好的有序ActiveRecord集合,我可以这样显示?

书名 - 19票

书名 - 15票

书名 - 9票

书名 - 8票

ruby mysql sql activerecord ruby-on-rails

5
推荐指数
2
解决办法
4910
查看次数

SSE/EventSource在第一块数据(Rails 4 + Puma + Nginx)之后关闭

我按照401-ActionController-Live Railscast和这篇博客文章关于Server-Sent-Events在我的Rails应用程序中设置类似的内容.当我打开与服务器的连接时,只有使用puma但是puma + nginx,在发送第一个数据块后连接关闭时,它才能正常工作.

我也尝试过按照这些问题提供的解决方案,但它们对我不起作用:

这就是我得到的:

卷曲反应

这是我如何设置我的服务器,这是我当前的nginx配置:

upstream puma {
  server unix:///home/deploy/apps/outy/shared/tmp/sockets/outy-puma.sock;
  keepalive 16;
}

server {
  listen 80 default_server deferred;

  root /home/deploy/apps/outy/current/public;
  access_log /home/deploy/apps/outy/current/log/nginx.access.log;
  error_log /home/deploy/apps/outy/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;

    proxy_http_version 1.1;
    proxy_set_header Connection ""; …
Run Code Online (Sandbox Code Playgroud)

nginx server-sent-events puma ruby-on-rails-4

5
推荐指数
1
解决办法
1225
查看次数

从Elixir中的Map将值加​​载到Struct中

假设我map有一些用户数据:

iex(1)> user_map
#=> %{name: "Some User", email: "user@example.com", password: "*********"}
Run Code Online (Sandbox Code Playgroud)

如何将其加载到%User{}结构中(希望使用一些Rubyish Elixir Magic)?


我目前试过这些但是所有这些都失败了.浏览Elixir网站上的Structs部分.

user_struct = %{ %User{} | user_map }
user_struct = %{ %User{} | Enum.to_list(user_map) }
Run Code Online (Sandbox Code Playgroud)

elixir

5
推荐指数
1
解决办法
2688
查看次数

将映射转换为 URI 查询字符串

我有一个长生不老药Map

m1 = %{a: 1, b: "fsfd", c: 123}
Run Code Online (Sandbox Code Playgroud)

我想将其转换为 的查询字符串URI

a=1&b=fsfd&c=123
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?我试过这个:

Enum.reduce(m1, fn({k, v}, acc) -> "#{acc}&{k}=#{v}" end)
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为acc没有按预期进行模式匹配。

reduce elixir

5
推荐指数
1
解决办法
2410
查看次数

长生不老药 UUID。UUID不匹配时如何处理500错误

def show(conn, %{"id" => id}) do
  with {:ok, user} <- UserAction.get_user(id)
    |> put_status(200)
    |> render("show.json", %{user: user})
  else
    {:error, :not_found} -> {:error, :not_found, %User{id: id}}
  end
end
Run Code Online (Sandbox Code Playgroud)

当 id 无效时,Ecto引发:

Ecto.Query.CastError - cannot be dumped to type :binary_id in query. 
Run Code Online (Sandbox Code Playgroud)

我的get_user功能:

query = from(u in User, where u.id == ^id)

case Repo.all(query) do
  [%User{} = user] -> {:ok, user}
  _ -> {:error, :not_found}
end
Run Code Online (Sandbox Code Playgroud)

是否有任何方便的方法来处理此错误以防止 500 个响应?

postgresql uuid elixir phoenix-framework

5
推荐指数
2
解决办法
1513
查看次数

Rails/ruby​​使用给定距离和从起点开始的方位计算新坐标

在ruby/rails中有一个简单的非数学类和确定的方法,输入是:

1) An Initial Geo-Coordinate 
2) A Bearing
3) A Distance in the Bearing's Direction
Run Code Online (Sandbox Code Playgroud)

输出是一个新的 Geo-Coordinate

我看过Ruby Geocoder Gem,但它没有这样做.

谢谢!

ruby geometry geolocation ruby-on-rails-3

4
推荐指数
1
解决办法
1542
查看次数

DropZone 在 init 中替换图像

我使用此代码从 init 中的数据库上传图像。现在我想在上传新图像时删除这个初始图像。

 var o = $("div#uploader").dropzone({
            url: "../../Merchant/UploadLogo",
            paramName: "logo",
            maxFiles: 1,

            //enqueueForUpload: false,
            maxfilesexceeded: function (file) {
                this.removeAllFiles();
                this.addFile(file);
            },

            addRemoveLinks: true,
            uploadMultiple: false,
            dictRemoveFile: "???",

            removedfile: function(file) {
                RemoveFile("Logo");
                var _ref;
                return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
            },

            init: function() {

                var mockFile = { name: "avatar1.jpg", size: 12345, type: 'image/jpeg', url: "../../Merchant/GetLogo" };
                this.options.addedfile.call(this, mockFile);
                this.options.thumbnail.call(this, mockFile, mockFile.url);//uploadsfolder is the folder where you have all those uploaded files
            }

        });
Run Code Online (Sandbox Code Playgroud)

javascript jquery dropzone.js

4
推荐指数
1
解决办法
4559
查看次数

Postgrex - 主机"xxx.xxx.xxx.xxx"没有pg_hba.conf条目

我有一个单独托管的Postgres数据库,我正在尝试使用我的Phoenix应用程序.我的prod配置是:

config :my_app, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: "postgres://username:password@myhost:5432/my_database",
  size: 20 
Run Code Online (Sandbox Code Playgroud)

当我的应用程序托管在Heroku上时,这工作正常,但是因为我已经将它移动到VPS,我不断收到此错误:

17:07:13.665 [info] GET /subjects
17:07:13.707 [info] Processing by MyApp.SubjectController.index/2
  Parameters: %{"format" => "html"}
  Pipelines: [:browser, :authorize]
17:07:13.951 [error] GenServer #PID<0.515.0> terminating
** (exit) %Postgrex.Error{message: nil, postgres: %{code: :invalid_authorization_specification, file: "auth.c", line: "474", message: "no pg_hba.conf entry for host \"xxx.xxx.xxx.xxx\", user \"username\", database \"my_database\", SSL off", pg_code: "28000", routine: "ClientAuthentication", severity: "FATAL"}}
17:07:13.970 [info] Sent 500 in 305ms
17:07:13.972 [error] #PID<0.513.0> running MyApp.Endpoint terminated
Server: xxx.xxx.xxx.xxx:80 (http)
Request: …
Run Code Online (Sandbox Code Playgroud)

postgresql ecto phoenix-framework

4
推荐指数
1
解决办法
3697
查看次数

整数列表在Elixir中打印为String

我正在努力Enum.map.当我向列表的所有元素添加100时,我发现了这个奇怪的输出.

终端截图

为什么这样输出?事实证明,当我添加100时我得到一个字符串但是当我做其他操作时工作得很好.我摆弄了一些,我仍然得到了这样的意外结果.

终端截屏2

elixir

4
推荐指数
1
解决办法
770
查看次数

如何在长生不老药中获得上个月

如何在不使用 elixir 中的包或库的情况下获得上个月的数据?

例如,如果当前日期是2018-01-25,我将得到2017-12-25。或者如果当前日期是2018-03-31,我会得到2018-02-28(2018 年不是闰年)

erlang datetime calendar elixir

4
推荐指数
1
解决办法
1358
查看次数