params.require(:task).permit(:summary, comments_attributes: [:id, :content])
我想在注解属性中添加用户ID和项目ID。
user_id = current_user.id
project_id = project.id
我试过下面的,但不起作用
params.require(:task).permit(:summary, comments_attributes: [:id, :content]).merge(user_id: current_user.id, comments_attributes: [user_id: current_user.id, project_id: project.id])
请帮帮我我怎么能这样做?
5条答案
按热度按时间23c0lvtd1#
虽然是一个老问题,正确的答案IMHO是这样的-〉
在Rails 5中,您应该使用reverse_merge而不是
.to_h.deep_merge
mrzz3bfm2#
你必须使用deep_merge
nxagd54h3#
首先将允许的参数转换为哈希,然后深度合并哈希:
7d7tgy0s4#
35g0bw715#
我无法使current top answer工作。
reverse_merge
本身似乎不会迭代地将reverse_merge
向下调用到ActionController::Parameters
的嵌套comments_attributes
版本。很高兴听到是否有更直接的答案!