如何从此ActionDispatch获取文件名

当我上传文件并请求以下参数时: params[:product][:images_attributes][“0”][:image] 它返回: #<ActionDispatch::Http::UploadedFile:0x007fcbe5c8f768 @original_filename="motorized-bicycle-9.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[images_attributes][0][image]\"; filename=\"motorized-bicycle-9.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#> 我将使用什么ruby代码来获取filename ?

从String中删除重复的数字和运算符

我正在尝试使用一个简单的数学表达式字符串,删除所有空格,删除所有重复的运算符,转换为单个数字,然后进行求值。 例如,类似“2 7 + * 3 * 95”的字符串应转换为“2 + 3 * 9”,然后评估为29。 这是我到目前为止所拥有的: expression.slice!(/ /) # Remove whitespace expression.slice!(/\A([\+\-\*\/]+)/) # Remove operators from the beginning expression.squeeze!(“0123456789”) # Single digit numbers (doesn’t work) expression.squeeze!(“+-*/”) # Removes duplicate operators (doesn’t work) expression.slice!(/([\+\-\*\/]+)\Z/) # Removes operators from the end puts eval expression 不幸的是,这不会产生单个数字,也不会像我预期的那样删除重复的运算符。 有任何想法吗?

使用Ruby将String的开头或结尾与子字符串进行比较的最快方法是什么?

切片字符串”Hello world!”[0, 5] == ‘Hello’ 0,5 “Hello world!”[0, 5] == ‘Hello’是Ruby中常用的一种习惯用法,用于将字符串的前n个或后n个字符与另一个字符串进行比较。 正则表达式也可以这样做。 然后是start_with? 和end_with? 这也可以做到这一点。 我应该以最快的速度使用哪种?

Ruby:在字符串中查找前N个正则表达式匹配(并停止扫描)

想要扫描非常长的字符串以进行正则表达式匹配。 想知道找到第一个N正则表达式的最有效方法是什么。 例如: ‘abcabcabc’.scan /b/, limit: 2 如果只扫描支持限制选项,则会在5个字符后成功结束。 (该字符串是几MB – 内存中的一个memoized数据结构 – 这是一个Web请求.Per很重要。)

如何将对象转换为字符串以用于ActiveRecord查询?

我有一个User ,它有一个String email属性。 但是,当我在我的应用程序中处理电子邮件时,我发现首先将其转换为(非持久的) Email对象是可取的,如下所示: class User < ActiveRecord::Base def email Email.new(self.read_attribute :email) end end Email#to_s Email#to_str和Email#to_str都被定义为原始字符串(例如, Email#to_str ),因此无论客户是处理Email还是String ,它通常都非常透明。 使用ActiveRecord分配属性时,这非常有效: > email = Email.new(‘foo@bar.com’) > user.email = email ActiveRecord知道email属性是一个字符串并相应地转换Email对象。 有点令人费解的是,在查询数据库时它不会这样做: > email = Email.new(‘foo@bar.com’) > User.find_by email: email ActiveRecord::StatementInvalid: can’t cast Email to string 显然,我可以打电话 > User.find_by email: email.to_s 但有没有办法让这个演员自动发生?

如何测试秩序意识的哈希平等

Ruby 1.9.2将顺序引入哈希。 考虑到顺序,我如何测试两个哈希值是否相等? 鉴于: h1 = {“a”=>1, “b”=>2, “c”=>3} h2 = {“a”=>1, “c”=>3, “b”=>2} 我想要一个比较运算符,它为h1和h2返回false 。 以下两点都不起作用: h1 == h2 # => true h1.eql? h2 # => true

Rails添加两个具有活动记录结果的范围

我正在使用act-as-taggable-on gem 我使用单个字段按tag_name和user_name进行搜索 User.rb class User “INNER JOIN taggings ON taggings.taggable_id = user.id\ INNER JOIN tags ON tags.id = taggings.tag_id AND taggings.taggable_type = ‘User'”, :conditions => [“tags.name = ?”, tag], :order => ‘id ASC’ } } def self.search(search) if search where(‘name LIKE ?’, “%#{search}%”) + tagged_with(search) else scoped end end end 但我有分页问题,​​而得到这个数组,我在config / initializer / […]

gem install:错误:无法构建gem原生扩展

我使用的是Arch Linux并安装了Ruby(这是2.0.0版本)。 当我安装Ruby时,它说我应该添加: $(ruby -rubygems -e “puts Gem.user_dir”)/bin 在我的路上,我把它添加到.bashrc 当我尝试gem安装rails或任何其他gem时,它会给出错误:无法构建gem原生扩展。 这是完整的错误: ERROR: While executing gem … (Gem::FilePermissionError) You don’t have write permissions for the /usr/lib/ruby/gems/2.0.0 directory. [alexander@localhost ~]$ sudo gem install rails –no-user-install Building native extensions. This could take a while… ERROR: Error installing rails: ERROR: Failed to build gem native extension. /usr/bin/ruby extconf.rb creating […]

PG ::错误:错误:关系schema_migrations的权限被拒绝

不知道为什么我遇到这个错误。 我正确设置了postgresql,只是运行了一个迁移,然后rake db:migrate,我得到了标题错误。 这是我的: database.yml的 development: adapter: postgresql encoding: unicode database: my_blog_development pool: 5 username: my_blog password: test: adapter: postgresql encoding: unicode database: my_blog_test pool: 5 username: my_blog password: production: adapter: postgresql encoding: unicode database: my_blog_production pool: 5 username: my_blog password: 完整错误: [my_blog]$rake db:migrate rake aborted! PG::Error: ERROR: permission denied for relation schema_migrations : SELECT […]

以编程方式将ELPC节点添加到ELB

以下代码旨在将VPC中新部署的EC2实例添加到负载均衡器。 (这显然是测试/虚拟代码,但实际工作流程在实际系统中是相同的) require ‘yaml’ gem ‘aws-sdk’,’1.6.0′ require ‘aws-sdk’ AWS.config({ :access_key_id => KEY, :secret_access_key => SECRET_KEY }) ec2 = AWS::EC2.new elb = AWS::ELB.new i = ec2.instances[“i-abcd1234”] elb.load_balancers[“lb-name”].instances.register(i) 当我运行此代码时,我收到此错误: AWS::ELB::Errors::ValidationError: ELB in VPC is not supported in this version of API. Please try 2011-11-15 or newer. 我的aws-sdk gem是RubyGems提供的最新版本,本周更新了。 这不是Ruby SDK for AWS中可用的function吗? 如何通过此gem或其他方式将VPC节点添加到带有Ruby代码的Load Balancer?