2017-10-16 42 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 –

相關問題