Rails - 在表单中获取 3 个 ID
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要 3 个组件才能正确创建荣誉记录,则需要从表单中传递它们。您似乎正确地完成了该部分。
要创建记录,请访问传递的变量。
理解上述内容并不是最有效的,但用于演示目的。您可能希望避免冗余的数据库命中,例如
: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.
To create the record, access the passed variables.
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