小编eng*_*nky的帖子

Ruby on Rails:提交表单中的数组

我有一个具有Array属性的模型.从表单提交中填充该属性的正确方法是什么?

我知道有一个带有字段的表单输入,其名称包括括号,从输入创建一个哈希.我应该只是拿着它并在控制器中踩过它来按摩它成阵列吗?

让它不那么抽象的例子:

class Article
  serialize :links, Array
end
Run Code Online (Sandbox Code Playgroud)

links变量采用URL数组的形式,即 [["http://www.google.com"], ["http://stackoverflow.com"]]

当我在表单中使用类似下面的内容时,会创建一个哈希:

<%= hidden_field_tag "article[links][#{url}]", :track, :value => nil %>
Run Code Online (Sandbox Code Playgroud)

结果哈希看起来像这样:

"links" => {"http://www.google.com" => "", "http://stackoverflow.com" => ""}
Run Code Online (Sandbox Code Playgroud)

如果我没有在链接名称中包含url,则其他值会相互冲突:

<%= hidden_field_tag "article[links]", :track, :value => url %>
Run Code Online (Sandbox Code Playgroud)

结果如下: "links" => "http://stackoverflow.com"

forms arrays hash ruby-on-rails

66
推荐指数
5
解决办法
8万
查看次数

rspec测试载波 - 新手

我正在使用carrierwave制作rails 3.1应用程序将文件上传到aws s3.我已按照carrierwave github存储库上的说明操作,现在可以将文件上传到我的aws存储桶.这是让我陷入困境的测试.在过去的两天里,我一直在谷歌搜索和修改,使用我发现的所有其他问答,但我终于哭了'妈妈'.这是我得到的:

/app/uploaders/image_file_uploader.rb

storage :fog

def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
Run Code Online (Sandbox Code Playgroud)

/config/initializers/carrierwave.rb

if Rails.env.test? or Rails.env.cucumber?
  CarrierWave.configure do |config|
    config.storage = :file
    config.enable_processing = false
  end
end
Run Code Online (Sandbox Code Playgroud)

/spec/uploaders/image_file_uploader_spec.rb

require 'spec_helper'
require 'support/fog_helper'
require 'carrierwave/test/matchers'

describe ImageFileUploader do
  include CarrierWave::Test::Matchers

  before do
    ImageFileUploader.enable_processing = true
    @user = Factory(:user, :email => "photo_taker@example.edu")
    @uploader = ImageFileUploader.new(@user, Factory(:image))
    @uploader.store!(File.open("#{Rails.root}/tmp/uploads/#{Rails.env}/images/"))
  end

  after do
    @uploader.remove!
    ImageFileUploader.enable_processing = false
  end

  context 'the tiny version' do
    it "should scale down a landscape image to be exactly 50 by …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails carrierwave ruby-on-rails-3.1

19
推荐指数
1
解决办法
8364
查看次数

如何在Amazon Linux系统中升级ruby版本?

我使用padrino ruby​​框架开发了ruby应用程序.我想在亚马逊上部署它.

我用过这张图片.Amazon Linux AMI 2017.09.1(HVM),SSD卷类型

图像描述是:

"Amazon Linux AMI是由EBS支持的AWS支持的映像.默认映像包括AWS命令行工具,Python,Ruby,Perl和Java.这些存储库包括Docker,PHP,MySQL,PostgreSQL和其他软件包."

启动实例后,我测试了ruby版本,ruby 2.0.0p648 (2015-12-16) [x86_64-linux]但是我需要ruby2.4来部署padrino框架.我试图更新ruby版本一整天都没有成功.console说当前版本是最新版本而且没有ruby2.4如何解决这个问题?(我将gem版本更新为2.7.2)

ruby amazon-web-services padrino

8
推荐指数
2
解决办法
6004
查看次数

Ruby 2.7 说 URI.escape 已过时,用什么替代它?

我正在使用URI.encode生成 HTML 数据 URL:

visit "data:text/html,#{URI::encode(html)}"
Run Code Online (Sandbox Code Playgroud)

升级到 Ruby 2.7.1 后,解释器开始警告:

warning: URI.escape is obsolete
Run Code Online (Sandbox Code Playgroud)

推荐的替代品是CGI.escapeURI.encode_www_form_component。然而,他们并没有做同样的事情:

2.7.1 :007 > URI.escape '<html>this and that</html>'
(irb):7: warning: URI.escape is obsolete
 => "%3Chtml%3Ethis%20and%20that%3C/html%3E"
2.7.1 :008 > CGI.escape '<html>this and that</html>'
 => "%3Chtml%3Ethis+and+that%3C%2Fhtml%3E"
2.7.1 :009 > URI.encode_www_form_component '<html>this and that</html>'
 => "%3Chtml%3Ethis+and+that%3C%2Fhtml%3E"
Run Code Online (Sandbox Code Playgroud)

这些轻微编码差异的结果 - html 页面,其中空格被替换为+. 我的问题是 -URI.encode这个用例有什么好的替代品?

ruby

8
推荐指数
3
解决办法
1651
查看次数

在 ruby​​ 类中同时创建同名的实例方法和类方法

有没有办法让我在 ruby​​ 类中同时创建同名的类和实例方法?我在课堂上创建了一个版本Foo

class Foo 
   def self.bar
      "hello world" 
   end

   def bar
     self.class.bar
   end
end
Run Code Online (Sandbox Code Playgroud)

虽然这可行,但是否有更优雅的方法来实现这一目标?现在,我必须复制约 10 个方法作为实例和类方法。

ruby ruby-on-rails

6
推荐指数
1
解决办法
1233
查看次数

Macbook M1 - Postgres 安装 - “/tmp/.s.PGSQL.5432” - 执行时失败;`/bin/launchctl 引导程序

我刚买了一台新的 Macbook Pro M1,并一直在尝试使用自制软件安装 Postgres。在尝试了一系列不同的方法(从安装不同版本的 Postgres 到尝试)后,rm /usr/local/var/postgres/postmaster.pid我没有运气。我现在甚至无法在没有错误消息的情况下成功安装 Postgres。当我卸载然后重新安装时,brew install postgres我得到:

\n
\xe2\x9e\x9c  ~ brew install postgres\n==> Downloading https://ghcr.io/v2/homebrew/core/postgresql/manifests/14.1_1\nAlready downloaded: /Users/landonlapensee/Library/Caches/Homebrew/downloads/34e850b9c47867f84334b48fe844f7a171369d777b5c118a4119297f7c215147--postgresql-14.1_1.bottle_manifest.json\n==> Downloading https://ghcr.io/v2/homebrew/core/postgresql/blobs/sha256:027c8b48406c3d732241426e0f5d2caf9f48cb4d2d38610b8f5d46f0adf7a89f\nAlready downloaded: /Users/landonlapensee/Library/Caches/Homebrew/downloads/b74d7a88df70e29d3cd5714896c2dd5b16a1e8c01188164026a4b5762f18234b--postgresql--14.1_1.monterey.bottle.tar.gz\n==> Pouring postgresql--14.1_1.monterey.bottle.tar.gz\n==> /usr/local/Cellar/postgresql/14.1_1/bin/initdb --locale=C -E UTF-8 /usr/local/var/postgres\nLast 15 lines from /Users/landonlapensee/Library/Logs/Homebrew/postgresql/post_install.01.initdb:\nData page checksums are disabled.\n\nfixing permissions on existing directory /usr/local/var/postgres ... ok\ncreating subdirectories ... ok\nselecting dynamic shared memory implementation ... posix\nselecting default max_connections ... 20\nselecting default shared_buffers ... 400kB\nselecting default time zone ... America/Vancouver\ncreating configuration files ... ok\nrunning bootstrap script ... …
Run Code Online (Sandbox Code Playgroud)

postgresql macos homebrew

6
推荐指数
1
解决办法
4043
查看次数

Ruby CSV:如何读取制表符分隔的文件?

CSV.open(name, "r").each do |row|
  puts row
end
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

CSV::MalformedCSVError Unquoted fields do not allow \r or \n 
Run Code Online (Sandbox Code Playgroud)

该文件的名称是.txt制表符分隔文件.我特意做了.我有一个.csv文件,我去了excel,并将文件保存为.txt标签分隔.所以它是制表符分隔的.

应该无法CSV.open读取制表符分隔的文件?

ruby csv tab-delimited

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

无法使用 Rbenv 安装 Ruby 3.2.0 -- 缺少 Pysch gem

我目前正在尝试使用 Rbenv 安装 Ruby 3.2.0,但每次都会收到此错误。

BUILD FAILED (Ubuntu 22.04 using ruby-build 20230202)
Inspect or clean up the working tree at /tmp/ruby-build.20230202161804.1244.1gO1Ch

Results logged to /tmp/ruby-build.20230202161804.1244.log 
Last 10 log lines:
Fix the problems, then remove these directories and try again if you want.
make[1]: Leaving directory '/tmp/ruby-build.20230202161804.1244.1gO1Ch/ruby-3.2.0'
Generating RDoc documentation
/tmp/ruby-build.20230202161804.1244.1gO1Ch/ruby-3.2.0/lib/yaml.rb:3: warning: It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby. uh-oh! RDoc had a problem:
cannot load …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails rbenv

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

Ruby API响应较低的驼峰案例

我正在尝试将API响应作为lowerCamelCase返回,但它不起作用,我需要为我的所有控制器/字段执行此操作,因此我需要整个项目的解决方案.

我已经尝试了很多东西,包括这个(http://brentvatne.ca/automatic-casing-activemodel-serializer/)谁告诉我将Activemodel配置为lower_camel如下

ActiveModel::Serializer.config.key_format = :lower_camel
Run Code Online (Sandbox Code Playgroud)

但这不起作用,它将返回以下json

{
    "users": [{
        "id": "56b110089c28691b84a3bd73",
        "first_name": "Lucas"
    }]
}
Run Code Online (Sandbox Code Playgroud)

我需要将first_name转换为firstName.

版本:

rails -v
Rails 4.2.5

ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [i386-mingw32]
Run Code Online (Sandbox Code Playgroud)

和宝石

active_model_serializers (0.10.0.rc4)
rails-api (0.4.0)
Run Code Online (Sandbox Code Playgroud)

我的ember App识别JSON,但我不想在JS上使用snake case变量

ruby json camelcasing ruby-on-rails active-model-serializers

4
推荐指数
2
解决办法
1812
查看次数

如何通过在最近使用的文本框中的光标处单击按钮来插入文本?

我有一个带有多个文本框和一组按钮的表单。使用下面的 Javascript,我可以单击一个按钮并将文本插入到其中一个命名框中。

单击按钮时,是否可以将文本插入到最近的/活动的文本框中?

目前我有这个,但它使用的是文本框的特定 ID,而不是最近使用/活动的 ID;

   $( 'input[type=button]' ).on('click', function(){
      var cursorPos = $('#my_text_box').prop('selectionStart');
      var v = $('#my_text_box').val();
      var textBefore = v.substring(0,  cursorPos );
      var textAfter  = v.substring( cursorPos, v.length );
      $('#my_text_box').val( textBefore+ $(this).val() +textAfter );
    });
Run Code Online (Sandbox Code Playgroud)

更清晰的 JSFiddle 示例;

http://jsfiddle.net/bd1xf0sj/1/

这个想法取决于单击按钮时您所在的文本框,而不是使用某个文本框的 ID。这个想法是可以单击按钮并且可以使用任何文本框。

javascript jquery

4
推荐指数
2
解决办法
309
查看次数