【发布时间】:2023-03-20 19:34:01
【问题描述】:
错误:
undefined method 'function_id' for #<Person:0xa004934>
有一个试验管理系统。到目前为止,用户已经能够创建新的参与者。现在用户也应该能够创建新的人了。该人也将成为试验的参与者。
我们有一个表格来创建一个新人。在表单中点击create person后,只有person表记录是OK的。但是participant 表中的function_id 属性是NULL。当用户创建一个人时,只需要在participant 表中设置值 function_id 是什么?因此,当创建一个新人时,应该始终创建一个参与者。
有 4 个表格:
- 试用
has_many :participants - 参与者
belongs_to人 - 人
has_many participants...has_many:试验,通过:参与者 - 函数
has_many参与者
目前有效:
使用/views/participants/new.html.erb 表单创建一个新参与者 (id | trial_id | function_id | person_id) 有效。使用/views/persons/new.html.erb 形式创建一个新人(id | title | prename | surname 等)不像上面提到的那样 100% 有效。
在persons/new.html.erb 形式中,用户应该能够在下拉框中选择一个选项,该选项是participant 的功能。
我不想要一个全新的participant 记录。我只想要 function_id 值。
下面是/persons/views/new.html.erb的sn-p
<%= form_for([@trial, @person]) do |f| %>
<div class="table">
<fieldset>
<div class="table">
<div class="tr">
<div class="th">
<%= f.label t(:function) %>
</div>
<div class="tdsep">:</div>
<div class="td">
<%= f.select :function_id, Function.all.collect {|fu| [fu.german, fu.id]} %>
</div>
</div>
...
编辑
人物模型
class Person < ActiveRecord::Base
belongs_to :organization
attr_accessible :organization_id,:title,:prename,:surname,:street,:street_number,:zip_code,:city,:phone,:fax,:email,:organization_description, :participants_attributes
has_many :participants
has_many :trials, through: :participants
人员控制器
class PersonsController < ApplicationController
load_and_authorize_resource :person, :through => :trial, :only => [:destroy]
before_filter :authenticate_user!, :except => [:index, :new, :create, :show]
load_and_authorize_resource :trial
def new
@person = Person.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @person}
end
end
def create
@person = Person.new(params[:person])
@person.trials << @trial
respond_to do |format|
if @person.save
format.html { redirect_to @person, notice: 'Person was successfully created.' }
format.json { render json: @person, status: :created, location: @person }
else
format.html { render action: "new" }
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end
提前致谢。
【问题讨论】:
标签: ruby-on-rails forms associations