我在引擎样式插件中有一些代码,其中包含一些模型.在我的应用程序中,我想扩展其中一个模型.我已经设法通过在初始化程序中包含一个模块,将实例和类方法添加到相关模型中.
但是,我似乎无法添加关联,回调等.我得到'找不到方法'错误.
/libs/qwerty/core.rb
module Qwerty
module Core
module Extensions
module User
# Instance Methods Go Here
# Class Methods
module ClassMethods
has_many :hits, :uniq => true # no method found
before_validation_on_create :generate_code # no method found
def something # works!
"something"
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
/initializers/qwerty.rb
require 'qwerty/core/user'
User.send :include, Qwerty::Core::Extensions::User
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个表单,允许我为关联输入分组的关联提交新记录.
class Product < AR::Base
has_many :properties
accepts_nested_attributes_for :properties
end
Run Code Online (Sandbox Code Playgroud)
请注意,在控制器中,为产品构建了一系列属性,因此@product.properties.empty? # => false.
下面fields_for给出了正确的输入,例如product[properties_attributes][0][value].
= form.fields_for :properties do |pform|
= pform.input :value
Run Code Online (Sandbox Code Playgroud)
但是一旦我尝试对关联进行分组,它就不再生成具有正确名称的输入:
- @product.properties.group_by(&:group_name).each do |group_name, properties|
%h3= group_name
= form.fields_for properties do |pform|
= pform.input :value
Run Code Online (Sandbox Code Playgroud)
这创建了name属性的输入,product[product_property][value]实际上它应该是product[property_attributes][0][value]第一个例子.
Rails文档建议您可以这样做:
= form.fields_for :properties_attributes, properties do |pform|
Run Code Online (Sandbox Code Playgroud)
但这会给出错误"Array的未定义方法值".
有没有一种方法是真正可选的,所以我知道它是否由调用者设置了吗?
将参数nil用作默认值是行不通的,因为无法知道调用方是否已通过nil或它是否为参数的默认值。
在命名参数之前,在Ruby 1.9中,使用options哈希:
def foo(options = {})
# …
bar(options)
end
def bar(options = {})
puts options.fetch(:name, ‘unknown’) # => 'unknown'
end
Run Code Online (Sandbox Code Playgroud)
使用Ruby 2.0命名参数:
def foo(name: nil)
# …
bar(name: name)
end
def bar(name: ‘unknown’)
# …
puts name # => nil, since nil is explicitly passed from `foo`
end
Run Code Online (Sandbox Code Playgroud) 我有一个通过常规上传的文件form_for,这给了我一个ActionDispatch::Http::UploadedFileparams哈希的对象,我可以调用它.read来获取内容.我现在需要将该文件嵌入XML文档中.我现在使用常规的Ruby字符串来构造XML.Rails字符串的默认编码是utf-8.
因此,我得到的错误Encoding::UndefinedConversionError,"\x89" from ASCII-8BIT to UTF-8.
对于以下文件会发生这种情况:
what-matters-now-1.pdf: application/octet-stream; charset=binary example.csv: text/plain; charset=utf-8 investigations.png: image/png; charset=binary
它不会发生:
my_test.txt: text/plain; charset=us-ascii
我试过改变编码,但是我得到了同样的错误:
params[:file].read.encode('utf-8')
Run Code Online (Sandbox Code Playgroud) 我想根据请求对象调用skip_before_filter.
伪代码:
skip_before_filter :authorize_user, :if => lambda { |controller| controller.request.ip == '127.0.0.1'
Run Code Online (Sandbox Code Playgroud)
这是可能的,似乎你只得到:只有/:除了skip_filter之外.
def my_method(options = {})
# ...
end
# => Syntax error in ./src/auto_harvest.cr:17: for empty hashes use '{} of KeyType => ValueType'
Run Code Online (Sandbox Code Playgroud)
虽然这是有效的Ruby似乎不是在Crystal中,但我怀疑是因为打字.如何告诉编译器我想要默认为空哈希?
将eol($)分配给键(^)bol左侧的键是否有合理的逻辑?
从物理上讲,"eol"位于右侧,"bol"位于左侧,是否更难以将关键地图反转以匹配屏幕上的内容?
我真的在问为什么键映射是这样的,我必须遗漏一些东西......
$ gem -v
1.8.5
$ ruby -v
ruby 1.8.7 (2010-04-19 patchlevel 253) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2010.02
$ sudo gem install sanitize
ERROR: Error installing sanitize:
nokogiri requires Ruby version >= 1.8.7
Run Code Online (Sandbox Code Playgroud)
这也适用于其他宝石,而不仅仅是nokogiri.我在Ruby 1.8.7上并/opt/ruby-enterprise-1.8.7-2010.02/bin/ruby在/usr/bin安装系统Ruby(1.8.6)之前设置了我的$ PATH .
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.5
- RUBY VERSION: 1.8.7 (2010-04-19 patchlevel 253) [x86_64-linux]
- INSTALLATION DIRECTORY: /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /opt/ruby-enterprise-1.8.7-2010.02/bin/ruby
- EXECUTABLE DIRECTORY: /opt/ruby-enterprise-1.8.7-2010.02/bin
$ which ruby
/opt/ruby-enterprise-1.8.7-2010.02/bin/ruby
$which gem
/opt/ruby-enterprise-1.8.7-2010.02/bin/gem
Run Code Online (Sandbox Code Playgroud) 这些是预期的:
-3 # => -3
0 - 3 # => -3
Run Code Online (Sandbox Code Playgroud)
但是这里发生了什么:
- 3 # => -3
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是-隐含的方法self,即self.-(3)如此定义的Kernel或Object.但尝试这样做会导致NoMethodError: undefined method.
我有三个类似的函数,他们过滤一组与键(列)匹配的映射与其值,不区分大小写.
这是我想要干的原始代码:
;; gets a collection filtered by a column, exact match
(defn get-collection-by-equals [collection-name column-name column-value]
(filter #(= (string/lower-case (column-name %)) (string/lower-case column-value)) (cached-get-collection collection-name)))
;; gets a collection filtered by a column, with partial match, case-insensitive
(defn get-collection-by-like [collection-name column-name column-value]
(filter #(string/includes? (string/lower-case (column-name %)) (string/lower-case column-value)) (cached-get-collection collection-name)))
;; gets a collection filtered by a column, which starts with given value, case-insensitive
(defn get-collection-by-starts-with [collection-name column-name column-value]
(filter #(string/starts-with? (string/lower-case (column-name %)) (string/lower-case column-value)) (cached-get-collection collection-name))) …Run Code Online (Sandbox Code Playgroud)