HABTM与Strong Parameters的关联未将用户保存在Rails 4中 [英] HABTM association with Strong Parameters is not saving user in Rails 4
本文介绍了HABTM与Strong Parameters的关联未将用户保存在Rails 4中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用户模型:
has_and_belongs_to_many :events
事件模型:
has_and_belongs_to_many :users
用户控制器:
params.require(:user).permit(:role, {:event_ids => []})
事件控制器:
params.require(:event).permit(:subject, :location, :date, :time, :all_day, :reminder, :notes, {:users_ids => []})
我的表单:
<%= simple_form_for(@event) do |f| %>
<%= f.input :subject %>
<%= f.input :location %>
<%= f.input :date %>
<%= f.input :time %>
<%= f.association :users %>
<%= f.input :all_day %>
<%= f.input :reminder %>
<%= f.input :notes %>
<%= f.button :submit, :class =>"btn btn-default" %>
<% end %>
我希望能够从列表中选择一个用户并将该用户保存到数据库中.但是,当我尝试创建一个新事件时,我在控制台中收到以下消息:
I want to be able to choose a user from a list and save that user to the database. But I am getting the following message in my console when I try to create a new Event:
Unpermitted parameters: user_ids
我的设置是否不正确?我没有将正确的实例变量传递给表单吗?
Do I have my setup incorrect? Am I not passing in the correct instance variables to the form?
推荐答案
U ...详细信息!
Ugh...details details!
以前如何:
params.require(:event).permit(:subject, :location, :date, :time, :all_day, :reminder, :notes, {:users_ids => []})
应该如何:
params.require(:event).permit(:subject, :location, :date, :time, :all_day, :reminder, :notes, {:user_ids => []})
请注意,:users_ids实际上应该是:user_ids!
Notice that :users_ids should actually be :user_ids!
这篇关于HABTM与Strong Parameters的关联未将用户保存在Rails 4中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文