我有一個人的表,其中一個人有一個電子郵件數組。 在我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類中關於電子郵件的唯一事情)。 @安德烈的回答完成了這項工作。 –