0

我有一個腳手架產品和兩個模型productnumbersserialnumber在導軌3中的嵌套形式錯誤,沒有正確保存

Product.rb

has_one :productnumber 
accepts_nested_attributes_for :productnumber 

Productnumber.rb

belongs_to :product 
has_many :serialnumbers 
accepts_nested_attributes_for :serialnumbers 

serialnumber.rb

belongs_to :productnumber 

的形式示出了精細和我能夠輸入數據,並創建或更新沒有問題但serialnumber根本沒有保存。編輯產品時,serialnumber字段爲空,但產品創建時有數據。

只保存productproductnumber,但不保存serialnumber

Rails不會給出serialnumber未保存的錯誤。任何幫助如何保存作爲productnumber的一部分的serialnumber

+4

請向我們展示您的窗體視圖代碼,以及您在控制器中創建的操作。 –

+0

@BenMiller,我是新來的stackoverflow..eventually我解決了這個問題,但現在我有一個新的概率,我會上傳控制器模型和形式 – Meow

+0

你記得添加attr_accessible到你的模型? –

回答

0
class CustomersController < ApplicationController 
    # GET /customers 
    # GET /customers.json 
    def index 
    @customers = Customer.find(:all, :include=> [:continent, :paymethod]) 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @customers } 
    end 
    end 

    # GET /customers/1 
    # GET /customers/1.json 
    def show 
    @customer = Customer.find(params[:id], :include=> [:continent, :paymethod]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @customer } 
    end 
    end 

    # GET /customers/new 
    # GET /customers/new.json 
    def new 
    @customer = Customer.new 
    @continents = Continent.find :all 
    @paymethods = Paymethod.find :all 
    @customer.build_company 



    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @customer } 
    end 
    end 


# GET /customers/1/edit 
    def edit 
    @customer = Customer.find(params[:id]) 
    @continents = Continent.find :all 
    @paymethods = Paymethod.find :all 

    end 

    # POST /customers 
    # POST /customers.json 
    def create 
    @customer = Customer.new(params[:customer]) 
    @continents = Continent.find :all 
    @paymethods = Paymethod.find :all 


    respond_to do |format| 
     if @customer.save 
     format.html { redirect_to @customer, notice: 'Customer was successfully created.' } 
     format.json { render json: @customer, status: :created, location: @customer } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @customer.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /customers/1 
    # PUT /customers/1.json 
    def update 
    @customer = Customer.find(params[:id]) 


    respond_to do |format| 
     if @customer.update_attributes(params[:customer]) 
     format.html { redirect_to @customer, notice: 'Customer was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @customer.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /customers/1 
    # DELETE /customers/1.json 
    def destroy 
    @customer = Customer.find(params[:id]) 
    @customer.destroy 

    respond_to do |format| 
     format.html { redirect_to customers_url } 
     format.json { head :no_content } 
    end 
    end 
end 
+0

請提供您的源代碼的一些解釋 –