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