Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveRecord::控制器中的AssociationTypeMismatch#创建_Ruby On Rails_Ruby On Rails 4_Activerecord_Haml - Fatal编程技术网

Ruby on rails ActiveRecord::控制器中的AssociationTypeMismatch#创建

Ruby on rails ActiveRecord::控制器中的AssociationTypeMismatch#创建,ruby-on-rails,ruby-on-rails-4,activerecord,haml,Ruby On Rails,Ruby On Rails 4,Activerecord,Haml,我正在Rails 4.2应用程序中编写一个基本表单,该应用程序可以提交一个表单供餐馆审查。但是,当我的测试运行时,我得到以下错误: Failure/Error: click_button "Submit review" ActiveRecord::AssociationTypeMismatch: Restaurant(#70225646207060) expected, got String(#70225618208620) # ./app/controllers/reviews_c

我正在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的创建方法:

  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
class Review
参数名为
餐厅
Review.create
将使用Review的
restaurant=
方法,该方法需要一个restaurant对象

如果您切换到使用
restaurant\u id
作为您的参数,它应该可以工作

# controller
Review.create!(params[:review].permit(:restaurant_id, :presentation, :service, :atmosphere, :comment))

# form
= f.collection_select(:restaurant_id, Restaurant.all, :id, :name)

我认为您正在尝试做的是:

= f.select :restaurant, Restaurant.pluck(:id, :name)

谢谢你的详细回复。但是,当我进行您建议的更改时,我会得到:ActionView::Template::Error:undefined method`restaurant_id'for#是否还有其他需要更改的内容?Review
是否属于
restaurant?是的,我在问题中添加了我的审阅模型。我还遗漏了什么?在控制台中,
Review.last.restaurant\u id
有效吗?好的,那不起作用。问题是我没有在表中创建餐厅id。一旦我在新的迁移中添加了该字段作为引用,一切都正常了。我没有从中得到任何错误,但是表单中下拉列表中的值只是ID。我将您的建议重新排序为(:name,:id),因此下拉列表显示了正确的值,但我仍然得到AssociationTypeMismatch:error。还有什么我应该看的吗?当我尝试时,我得到了:未定义的方法'restaurant\u id'#