我有一个人的表,其中一个人有一个电子邮件数组。 在我schema.rb它看起来像这样:水豚字符串阵列类型fill_in与工厂创建的数据失败
create_table "people", force: true do |t|
t.string "email", array: true
我的人,模型验证电子邮件的存在:validates :email, presence: true
我的人控制器创建人是这样的:
def create
@person = Person.new(person_params)
@person.email << person_params["email"] #Rails 4 strong parameters
respond_to do |format|
if @person.save!
format.html { redirect_to people_url, notice: t('app.people.successful_save') }
format.json { render :index, status: :created }
else
format.html { render :index}
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end
我的表单.html.haml询问邮件地址:
= f.label :person, t('app.people.email')
= f.text_field :email
,创建电子邮件(其中包括)厂件:
FactoryGirl.define do
factory :person do
# other stuff here...
email ["[email protected]"]
# ...and other stuff here
end
end
这里是我的功能规格失败:
it 'is able to create a new person' do
person = build(:person)
visit 'people'
click_button 'New Person'
within ("#new_person_form")do
# other passing stuff here...
fill_in 'person_email', :with => person.email # <----FAILURE HERE
# ...and other not passing stuff here
end
click_button 'Save'
expect(page).to have_content 'Person saved'
end
错误消息本身:
Failure/Error: fill_in 'person_email', :with => person.email
ArgumentError:
Value cannot be an Array when 'multiple' attribute is not present. Not a Array
如果我用Google搜索了这条信息,我发现这个: https://github.com/jnicklas/capybara/blob/master/lib/capybara/selenium/node.rb#L30 不幸的是我不太了解它。我还检查了水豚作弊表,以了解可能犯的错误,但这并不好。
我得到的规范来传递,如果我与某事任意这样的替换person.email: fill_in 'person_email', :with => "[email protected]"
我曾尝试不同类型的值的,包括使用和不使用阵列brackets-相同的错误消息在工厂电子邮件出现。
当我创建一个person对象而不是构建它并在工厂内的电子邮件字段中使用纯字符串而不是数组时,我得到了不同的消息,然后我的模型无法通过电子邮件状态验证。但我想这是合乎逻辑的,因为关于模式,模型假定获取数组而不是字符串。
我对RSpec还不是很有经验,所以也许这是一个简单的错误..无论如何需要帮助,谢谢!
UPDATE1: Person类的定义:
class Person < ActiveRecord::Base
validates :email, presence: true
end
什么是人的类定义?基于这个错误,'email'方法返回一个Array而不是String。 –
更新了我的问题(验证是Person类中关于电子邮件的唯一事情)。 @安德烈的回答完成了这项工作。 –