开发者

Rails - Get 3 ID's in a form

开发者 https://www.devze.com 2023-03-08 05:26 出处:网络
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 t

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_gr开发者_StackOverflowoupmembership.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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消