없이 작업을 Belongs_to 수, 나는 모든 주문 고객에 속한다는 점에서, 다음과 같은 모델을 사용하고, 그래서 만들 때 순서 모델에 순서이 has_many 또는 has_one
을 오류를 제공 belongs_to 사용했다 주문이 잘 작동 고객 모델에서, 그것은 만 belongs_to 작동하지 않는 이유 : #
내가 has_many를 사용 에 대한
정의되지 않은 메서드 '주문'
has_many를 사용한 작업 : 고객 모델의 주문은 이 아닙니다. has_one : 고객 컨트롤러에서 위와 동일한 오류가 발생합니다.
미리 감사드립니다.
모델 : - order.rb
class Order < ActiveRecord::Base
belongs_to :customer
attr_accessible :order_date, :customer_id
end
모델 : - customer.rb
class Customer < ActiveRecord::Base
attr_accessible :name
end
컨트롤러 : - orders.rb
def create
@customer = Customer.find_by_name(params[:name])
@order = @customer.orders.new(:order_date => params[:orderdate])
respond_to do |format|
if @order.save
format.html { redirect_to @order, notice: 'Order was successfully created.' }
format.json { render json: @order, status: :created, location: @order }
else
format.html { render action: "new" }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
감사합니다. Belongs_to has_many에 대해서는 잘 작동하지만 belongs_to has_one의 경우 오류가 발생합니다. - 정의되지 않은 메소드'new '.. @order = @ customer.order.new (: order_date => params [: orderdate]); @ order.save; –