我正在使用Rails 4.2.3和PostGre數據庫。我有一個有多個成員對象的對象。在我的應用程序/模型/ my_object.rb文件,我有NoMethodError嘗試保存子對象字段時的未定義方法
class MyObject < ActiveRecord::Base
belongs_to :user
has_many :my_object_times
has_one :address
attr_accessor :hour, :minute, :second
validates_numericality_of :overall_rank, :age_rank, :gender_rank
accepts_nested_attributes_for :my_object_times
end
在我看來
然後,我有這個在我的形式...
<%= f.fields_for :my_object_times do |rt| %>
<div class="field">
<%= rt.label :overall_rank %><br>
<%= rt.text_field :overall_rank %>
</div>
但我提交表單時,我得到這個錯誤
NoMethodError (undefined method `overall_rank' for #<MyObject:0x007fd66df43968>):
app/controllers/my_objects_controller.rb:10:in `block in create'
app/controllers/my_objects_controller.rb:9:in `create'
我不明白爲什麼我得到這個錯誤。下面是我的應用程序/控制器/ my_objects_controller.rb文件
class MyObjectsController < ApplicationController
def create
@my_object = MyObject.new(my_object_params)
@current_user = User.find(session["user_id"])
@my_object.user = @current_user
@date = Date.strptime(my_object_params[:day], "%m/%d/%Y")
@my_object.day = @date
respond_to do |format|
if @my_object.save
flash[:notice] = 'Saved successfully'
format.html { redirect_to controller: "users", action: "index", notice: 'Saved successfully.' }
format.js { render js: "window.location='/users'" }
else
format.html { render action: "index" }
format.js { render json: @my_object.errors, status: :unprocessable_entity }
end
end
end
任何幫助表示讚賞, - 戴夫