我是一名RoR新手,在构建表单以更新两个模型时遇到了很多麻烦。我有一个非常简单的起点:产品和版本。以下是产品和版本模型:
class Product < ActiveRecord::Base
validates :name, :presence => true
has_many :versions, :dependent => :destroy
accepts_nested_attributes_for :versions, :allow_destroy => :true
end
类版本&lt;的ActiveRecord :: Base的
belongs_to:产品 端
以下是我对产品和版本的迁移:
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 %>
以下是上面的render partial语句中调用的版本表单:
<%= 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
我非常感谢指出我做错了什么。提前致谢