Rails - 在表单中获取 3 个 ID

发布于 2024-11-09 10:27:04 字数 889 浏览 2 评论 0原文

我有一个 group#view 页面,由 Person 访问。在这个页面中,Person可以通过我开发的方法看到group的成员。问题是我需要使用 group 中的 Id、访问页面的 person 中的 ID 以及来自此成员的id。

在我的 Honors 控制器中,我有:

def create
  @person = Person.find(current_person)
  @honor = Honor.create(:group => Group.find(params[:group_id]), 
  :person => Person.find(current_person), :honored => Person.find(current_person))
 if @honor.save
 ...
end

问题出在这个 :honored =>; Person.find(current_person),这没有获得正确的ID,我不知道如何获得它。

我认为:

 <% @asked_groupmembership.each do |agm| %>
 <% form_for(:honor, :url => honors_path(:group_id => @group.id, :person => current_person.id,:honor => agm.member.id)) do |f| %>

有什么帮助吗?

谢谢。

I have a group#view page, that is accessed by a Person. In this page, the Person can see the members of the group via methods I developed. The problem is that I need to create the model Honors using the Id from the group, the id from the person accessing the page, and the id from a member of this group.

In my Honors controller I have:

def create
  @person = Person.find(current_person)
  @honor = Honor.create(:group => Group.find(params[:group_id]), 
  :person => Person.find(current_person), :honored => Person.find(current_person))
 if @honor.save
 ...
end

The problem is in this :honored => Person.find(current_person), that is not getting the right ID and I don`t know how to get it.

In my view:

 <% @asked_groupmembership.each do |agm| %>
 <% form_for(:honor, :url => honors_path(:group_id => @group.id, :person => current_person.id,:honor => agm.member.id)) do |f| %>

Any help?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

萧瑟寒风 2024-11-16 10:27:04

如果您需要 3 个组件才能正确创建荣誉记录,则需要从表单中传递它们。您似乎正确地完成了该部分。

:group_id => @group.id
:person => current_person.id
:honor => agm.member.id

要创建记录,请访问传递的变量。

Honor.create(:group => Group.find(params[:group_id]),
             :person => Person.find(params[:person]),
             :honored => Person.find(params[:honor]))

理解上述内容并不是最有效的,但用于演示目的。您可能希望避免冗余的数据库命中,例如 :person => current_person 而不是另一个查询

If you need 3 components to properly create an honor record, you need to pass them from the form. You seem to be doing that part correctly.

:group_id => @group.id
:person => current_person.id
:honor => agm.member.id

To create the record, access the passed variables.

Honor.create(:group => Group.find(params[:group_id]),
             :person => Person.find(params[:person]),
             :honored => Person.find(params[:honor]))

Understanding the above isn't the most efficient, but used for demonstrative purposes. You'd likely want to avoid redundant database hits, e.g. :person => current_person rather than another query

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文