2014-03-03 30 views
1

我想在包含连字符的字段名称的模型上创建一个工厂,但我无法弄清楚允许连字符的语法。我正在使用Mongoid。在字段名称中使用连字符定义工厂

#model.rb 
class MyModel 
    include Mongoid::Document 

    field :field1 
    field :"data-field2" 
    field :"data-field3" 
end 



#factories/my_model.rb 
FactoryGirl.define do 
    factory :my_model do 
    field1 'some text' 
    data-field2 'some_element_classname' 
    data-field2 'some_other_element_classname' 
    end 
end 

我得到这个错误

unexpected tSTRING_BEG, expecting keyword_do or '{' or '(' (SyntaxError) 

任何人都知道如何解决这个问题?

+0

可以尝试使用'send'?即“发送”data-field2“some_element_classname”等。 –

回答

2

FactoryGirl::DefinitionProxy定义method_missing使用缺少方法的名字来称呼add_attribute,所以你应该能够子,在代替:

FactoryGirl.define do 
    factory :my_model do 
    field1 'some text' 
    add_attribute(:"data-field2", 'some_element_classname') 
    add_attribute(:"data-field3") { # add_attribute with block } 
    end 
end 
+0

BINGO!谢谢Zack。 – SteveO7

相关问题