【问题标题】:Rails - Get 3 ID's in a formRails - 在表单中获取 3 个 ID
【发布时间】:2011-05-24 13:48:09
【问题描述】:

我有一个group#view 页面,可以通过Person 访问。在这个页面,Person可以通过我开发的方法看到group的成员。问题是我需要使用来自group 的ID、来自person 访问页面的ID 以及来自membergroup 的ID 来创建模型Honors

在我的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| %>

有什么帮助吗?

谢谢。

【问题讨论】:

  • current_person 来自哪里?
  • 这是工作它来自会话控制。访问该页面的人将是current_person。我知道在:honored =&gt; Person.find(current_person) 中,current_person 不应该在那里,但我只是不知道该放什么。

标签: ruby-on-rails ruby-on-rails-3 forms controller


【解决方案1】:

如果您需要 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 =&gt; current_person 而不是另一个查询

【讨论】:

  • 感谢您的回答!我会马上试试这个。我尝试了其他方法并提出了一个新问题,试图通过new form 传递这些 ID。你能看看吗? stackoverflow.com/questions/6112340/…
猜你喜欢
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多