通过新表单传递ID [英] Passing IDs through a new Form

查看:170
本文介绍了通过新表单传递ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于一个不同的方法,我试图对这里问:

This question is about a different approach I'm trying to the one asked here:

通过新表单传递ID

我有一个 #view page ,由 Person 访问。在此页面中, Person 可以通过我开发的方法查看组的成员。问题是,我需要使用来自的I​​D创建荣誉的模型,从 person 访问该网页,并从此组的成员 >

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)
  @asked_groupmembership = @person.owned_group_memberships.find_all_by_status(true,:include => [:group, :member])
  @asked_groupmembership.each do |agm|
  @honor = Honor.create(:group => Group.find(params[:group_id]), 
  :person => Person.find(current_person), :honored => Person.find(agm.member.id))
  end
 if @honor.save
 ...
end

在我看来,我有一个链接,指导到表单,以创建一个新的荣誉: / p>

In my view I have a link that directs the person to the form in order to create a new honor:

<% @asked_groupmembership.each do |agm| %>
  <%= link_to "Create Honor", new_honor_path(:group_id => @group.id, :person => current_person.id,
  :honored => agm.member.id) %> 

但在我的表单中,我无法获取ids和东西

But in my forms I can't get the ids and stuff

<% form_for(:honor, :url => honors_path(:group_id, :person,
 :honored)) do |f| %>

我得到的错误是我找不到没有ID的组。

The error I get is that I can't find Group without an Id.

任何想法?非常感谢。

## Edited2 ##

c> crontroller

Changed my crontroller

def new
  #@person = Person.find(params[:person])
  #@honored = Person.find(params[:honored])
  #@group = Group.find(params[:group_id])
  @honor = Honor.new
end


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


推荐答案

首先,你似乎缺少一个控制器方法。除了创建新对象的每个窗体都有两个控制器方法。

First, it seems like you are missing a controller method. Along with every form that creates a new object there are typically two controller methods

new


  • 收集表单需要呈现的任何数据

  • 呈现表单

创建


  • 收集表单中的数据

  • 创建新对象

它看起来像我一样缺少新的方法。在新方法中,您将收集表单需要的所有隐藏数据(例如,用户不打算直接输入的信息,例如@person信息)。然后,我会使用隐藏的表单参数(而不是尝试将其放在表单URL中)将此信息放在您的表单中。

It looks to me like you are missing the new method. In the new method you would gather up all the hidden data that the form needs (e.g. the information that the user is not going to type in directly, like the @person info). Then, I would put this information in your form using hidden form parameters (rather then trying to put it in the form URL).

这篇关于通过新表单传递ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆