Rails 强参数 - 使用来自 has_many 对象的字段 [英] Rails strong params - using fields from has_many object
本文介绍了Rails 强参数 - 使用来自 has_many 对象的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
非常感谢您的帮助.
在我的 rails 项目中,我有:
In my rails project I have:
位置模型,有很多:广告
class Location < ApplicationRecord
has_many :ads, :dependent => :destroy
end
广告模型
在控制器 developer_controller.rb 中,我需要使用 strong params 来自 location 和 ad<的字段/strong>
In the controller developer_controller.rb I need to use with strong params the fields from location and ad
params.require(:location).permit(:country, :location, {:ads => [:remote, :days]})
我不知道如何从广告对象中正确检索远程和天字段.
I don't know how to retrieve correctly the fields remote and days from the ad object.
非常感谢
克林顿
推荐答案
所有你需要使用的_attributes:
params.require(:location).permit(:country, :location, {:ads_attributes => [:remote, :days]})
这篇关于Rails 强参数 - 使用来自 has_many 对象的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文