Ruby on rails Rails 3.1.1中多模式表单的控制器代码

Ruby on rails Rails 3.1.1中多模式表单的控制器代码,ruby-on-rails,forms,controller,Ruby On Rails,Forms,Controller,我是一个RoR新手,在构建一个表单来更新两个模型时遇到了很多麻烦。我有一个非常简单的起点:产品和版本。以下是产品型号和版本型号: class Product < ActiveRecord::Base validates :name, :presence => true has_many :versions, :dependent => :destroy accepts_nested_attributes_for :versions, :allow_de

我是一个RoR新手,在构建一个表单来更新两个模型时遇到了很多麻烦。我有一个非常简单的起点:产品和版本。以下是产品型号和版本型号:

class Product < ActiveRecord::Base
    validates :name,  :presence => true
    has_many :versions, :dependent => :destroy
    accepts_nested_attributes_for :versions, :allow_destroy => :true
end
class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :name

      t.timestamps
    end
  end
end

class CreateVersions < ActiveRecord::Migration
  def change
    create_table :versions do |t|
      t.integer :number

      t.timestamps
    end
  end
end
我非常希望有人能指出我做错了什么。提前谢谢

class ProductsController < ApplicationController
  # GET /products
  # GET /products.json
  def index
    @products = Product.all

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

  # GET /products/1
  # GET /products/1.json
  def show
    @product = Product.find(params[:id])

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

  # GET /products/new
  # GET /products/new.json
  def new
    @product = Product.new
    @version = Version.new

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

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(params[:product])
    #@version = Version.new(params[:version])
    @version = @product.version.create(params[:version])    
    #redirect_to products_path(@product)

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

  # PUT /products/1
  # PUT /products/1.json
  def update
    @product = Product.find(params[:id])

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

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :ok }
    end
  end
end
<%= form_for(@product) do |f| %>
  <% if @product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
      <% @product.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
<%= render :partial => 'versions/form',
            :locals => {:form => f} %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
<%= form.fields_for :version do |v| %>
  <div class="field">
    <%= v.label :number, 'Number' %><br />
    <%= v.text_field :number %>
  </div>
<% end %>
ActiveRecord::UnknownAttributeError in ProductsController#create 

unknown attribute: version