【发布时间】:2015-03-23 05:35:57
【问题描述】:
我正在编写一个 Rails 4.2 应用程序中的基本表单,该应用程序提交表单以对餐厅进行审核。但是,当我的测试运行时,我收到以下错误:
Failure/Error: click_button "Submit review"
ActiveRecord::AssociationTypeMismatch:
Restaurant(#70225646207060) expected, got String(#70225618208620)
# ./app/controllers/reviews_controller.rb:12:in `create'
我知道有很多类似的问题,我已经尝试了很多解决方案,但到目前为止都没有奏效。这个错误表示什么,正确的解决方案是什么?
这里是 reviews_controller.rb 中的 create 方法:
def create
Review.create!(params[:review].permit(:restaurant, :presentation, :service, :atmosphere, :comment))
redirect_to root_path
end
新的审核表单(haml),我省略了其他文本输入字段:
= form_for @review do |f|
= f.label :restaurant
= f.collection_select(:restaurant, Restaurant.all, :id, :name)
= f.submit "Submit review"
review.rb 模型:
class Review < ActiveRecord::Base
belongs_to :restaurant
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 activerecord haml