嵌套属性不将 user_id 保存在连接表中

发布于 2024-12-09 05:04:20 字数 1689 浏览 0 评论 0原文

管理员主控制器:

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_atsent_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心旳酸楚 2024-12-16 05:04:20

使用:

params[:invitation].merge!(:sender_id => current_user.id)

注意 =>,而您只有 =。但是,在您的 create 操作中,您将其添加到 params[:invitation] 中,但您并未将其保存在任何地方,因此它只是丢失了。您可能想使用:

params[:admin][:invitations_attributes][:sender_id]

此外,您正在存储 sender_id 但没有任何信息告诉您的用户模型查找 sender_id 作为外键。默认情况下,它将查找user_id。您必须添加以下内容:

User: has_many :invitations, :foreign_key => 'sender_id'

以及:

Invitation: belongs_to :sender, :class_name => "User" # recommended
# or 
Invitation: belongs_to :user, :foreign_key => 'sender_id'

Use:

params[:invitation].merge!(:sender_id => current_user.id)

Notice the =>, whereas you just has =. However, in your create action, you are adding that to params[:invitation], but then you aren't saving that anywhere, so it's just getting lost. You probably want to use:

params[:admin][:invitations_attributes][:sender_id]

Also, you are storing sender_id but you don't have anything to tell your User model to look for sender_id as the foreign key. By default it will look for user_id. You have to add this:

User: has_many :invitations, :foreign_key => 'sender_id'

and either:

Invitation: belongs_to :sender, :class_name => "User" # recommended
# or 
Invitation: belongs_to :user, :foreign_key => 'sender_id'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文