嵌套属性不将 user_id 保存在连接表中
管理员主控制器:
def new
@admin = Admin.new
@invitation = @admin.invitations.build
@admi.user_admin_relationships.build
end
def create
params[:invitation][:sender_id] = current_user.id
@admin = Admin.new(params[:admin])
if @admin.save
redirect_to root_path
else
render 'new'
end
end
邀请模型
belongs_to :admin
belongs_to :user
管理员模型
has_many :invitations
has_many :user_admin_relationships
has_many :users, :through => :user_admin_relationships
accepts_nested_attributes_for :invitations
accepts_nested_attributes_for :user_admin_relationships, :allow_destroy => true
用户模型
has_many :invitations
has_many :user_admin_relationships
has_many :admins, :through => :user_admin_relationships
我的表单是嵌套表单,保存字段效果很好根据表单和项目,例如 created_at
和 sent_at
。 但是,它不会保存user_id
(user_admin加入表)和sender_id
(邀请表)。
我尝试向管理控制器添加不同的排列,但没有任何效果。
不起作用的事情:
params[:invitation][:sender_id] = current_user.id
这给了我一个 nil 对象,而我没想到它会出错。
params[:invitation].merge!(:sender_id = current_user.id)
这给了我一个“错误的语法错误”
@admin.invitations.build(params[:invitation][:sender_id]) = current_user.id
这给了我意外的“=”和“期望关键字结束”错误
我也尝试了一堆其他不同的排列。我的联想有问题吗?如何更新邀请表中的 sender_id
和丰富联接 user_admin_relationship 表中的 user_id
?我知道我可以通过 hidden_field
来做到这一点,但听说它不安全,所以我不想这样做。
Admin Main controller:
def new
@admin = Admin.new
@invitation = @admin.invitations.build
@admi.user_admin_relationships.build
end
def create
params[:invitation][:sender_id] = current_user.id
@admin = Admin.new(params[:admin])
if @admin.save
redirect_to root_path
else
render 'new'
end
end
Invitation Model
belongs_to :admin
belongs_to :user
Admin Model
has_many :invitations
has_many :user_admin_relationships
has_many :users, :through => :user_admin_relationships
accepts_nested_attributes_for :invitations
accepts_nested_attributes_for :user_admin_relationships, :allow_destroy => true
User Model
has_many :invitations
has_many :user_admin_relationships
has_many :admins, :through => :user_admin_relationships
My forms are nested forms and it works fine saving the fields per the form and items such as created_at
and sent_at
. However, it does not save the user_id
(user_admin join table) and sender_id
(invitation table).
I tried adding different permutations to the admin controller, but nothing is working.
Things that does not work:
params[:invitation][:sender_id] = current_user.id
This gives me I have a nil object when I didn't expect it error.
params[:invitation].merge!(:sender_id = current_user.id)
This give me a "wrong syntax error"
@admin.invitations.build(params[:invitation][:sender_id]) = current_user.id
This gives me unexpected "=" and "expecting keyword end" error
I have tried a bunch of other different permutations as well. Is there something wrong with my associations? How can I update the sender_id
in the invitation table and the user_id
in the rich join user_admin_relationship table? I know I can do it via a hidden_field
, but heard it's not safe so I don't want to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用:
注意
=>
,而您只有=
。但是,在您的create
操作中,您将其添加到params[:invitation]
中,但您并未将其保存在任何地方,因此它只是丢失了。您可能想使用:此外,您正在存储
sender_id
但没有任何信息告诉您的用户模型查找sender_id
作为外键。默认情况下,它将查找user_id
。您必须添加以下内容:以及:
Use:
Notice the
=>
, whereas you just has=
. However, in yourcreate
action, you are adding that toparams[:invitation]
, but then you aren't saving that anywhere, so it's just getting lost. You probably want to use:Also, you are storing
sender_id
but you don't have anything to tell your User model to look forsender_id
as the foreign key. By default it will look foruser_id
. You have to add this:and either: