Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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 以rails的形式传递集合\u选择和文本\u字段值_Ruby On Rails_Forms_Collections_Form For - Fatal编程技术网

Ruby on rails 以rails的形式传递集合\u选择和文本\u字段值

Ruby on rails 以rails的形式传递集合\u选择和文本\u字段值,ruby-on-rails,forms,collections,form-for,Ruby On Rails,Forms,Collections,Form For,吸引力模型属于目的地 我试图创建一个新的景点(text_field:name),但也将其链接到一个已经创建的目的地。这些目的地在下拉菜单中呈现,并带有此集合\u选择标记。通过单击提交,我希望吸引力被创建并用目的地外键保存在activerecord数据库中 f、 集合_选择(:吸引,:目的地_id,destination.all,:id,:name)%> 整个街区目前看起来是这样的: <h1>New attraction</h1> 新景点 选择一个城市 我如何用适当

吸引力模型属于目的地

我试图创建一个新的景点(text_field:name),但也将其链接到一个已经创建的目的地。这些目的地在下拉菜单中呈现,并带有此集合\u选择标记。通过单击提交,我希望吸引力被创建并用目的地外键保存在activerecord数据库中

f、 集合_选择(:吸引,:目的地_id,destination.all,:id,:name)%>

整个街区目前看起来是这样的:

<h1>New attraction</h1>
新景点
选择一个城市


我如何用适当的目的地将吸引力保存到数据库中?提前谢谢

我没有使用
f.collection\u select()
,因此不确定它在参数中调用了什么,但是让我们假设下面的代码是
params[:attraction][:destination\u id]
。您可以将创建操作更改为:

def create
  @destination = Destination.find(params[:attraction][:destination_id])
  @attraction = @destination.attractions.build(params[:attraction])
  if @attraction.save
    ...
  else
    ...
  end
end
这是假设您已经创建了一个
有许多
并且
属于
关联

如果遇到质量分配错误,请将第一行更改为:

@destination = Destination.find(params[:attraction].delete(:destination_id))
# this will return the deleted value, and in the next line of code
# you'll have the rest of the params still available
@destination = Destination.find(params[:attraction].delete(:destination_id))
# this will return the deleted value, and in the next line of code
# you'll have the rest of the params still available