2017-10-16 77 views
0

ERROR:未定义方法 - 属性是提交表单之后NULL(滑轨3.2.13)

undefined method 'function_id' for #<Person:0xa004934>

有一个试验管理系统。到目前为止,用户已经能够创建新的参与者。现在用户也应该能够创建新的人员。该人也应该参加审判。

我们有一个表单来创建一个新的人。在表单中输入create person后,只有人员表格记录可以。但participant表中的function_id属性为NULL。当用户创建一个人时,只需在participant表中设置值function_id?所以当创建一个新人时,应始终创建一个参与者。

有表:

  • 审判has_many :participants
  • 参与者belongs_to
  • has_many participants ... has_many:试验中,通过:参与者
  • 功能has_many参与者

目前为止的作品:

创建一个新的参与者(id | trial_id | function_id | person_id)以/views/participants/new.html.erb的形式工作。使用表格/views/persons/new.html.erb创建一个新人(id | title |姓氏|姓氏等)并不像上面提到的那样100%。 在表格persons/new.html.erb中,用户应该能够在下拉框中选择一个选项,该选项是participant的功能。

我不想要完整的新participant记录。我只想要function_id值。

下面是/persons/views/new.html.erb

<%= 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> 
    ... 

编辑

Person模型的一个片段

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 

PersonsController

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 

提前致谢。

回答

0

您必须将belongs_to :function添加到您的人物模型中。

+0

这并不解决问题见here。 #'仍然存在'未定义的方法function_id'。我是否也应该调整视图? –

+0

你可以用你的人物模型编辑你的文章,并通过你的控制器获取参数? –

+0

将'''belongs_to:function'''添加到人物模型,将'''function_id'''添加到attr_accessible –

0

您不能在人员新视图中添加function_id,因为人员db中没有任何function_id

所以解决办法是制作一个select_tag而不是f.select

<%= select_tag :function_id, options_from_collection_for_select(Function.all.collect {|fu| [fu.german, fu.id]}) %> 

更多信息

+0

我做到了。现在出现错误“人员中的参数错误#新 - 参数的错误数量(1为3)”。我只是读你的推荐来源。但其他两个参数是什么? –

+0

什么是您的rails版本? –

+0

我的rails版本是3.2.13 –