我是铁路的新手,我正在基于这个railscast构建一些东西,
https://github.com/railscasts/403-dynamic-forms
由于这已经非常过时了,我无法下载它所依赖的一个旧宝石——至少使用RVM——所以我无法真正检查它。
然而,有人将其从rails 3更新为4.2,https://github.com/asang/403-dynamic-forms,但我一直收到undefined method 'fields' for nil:NilClass
错误。
产品.rb
class Product < ActiveRecord::Base
belongs_to :product_type
serialize :properties, Hash
validate :validate_properties
def validate_properties
product_type.fields.each do |field|
if field.required? && properties[field.name].blank?
errors.add field.name, "must not be blank"
end
end
end
end
另一个问题是,当从rails 3更改为rails 4.2时,product_field.rb如何访问:field_type
,如果没有控制器,如何访问:name
?
Product_field.rb-Rails 3
class ProductField < ActiveRecord::Base
belongs_to :product_type
attr_accessible :field_type, :name, :required
end
Product_field.rb-Rails 4.2
class ProductField < ActiveRecord::Base
belongs_to :product_type
end
产品类型控制器.rb
class ProductTypesController < ApplicationController
def index
@product_types = ProductType.all
respond_to do |format|
format.html
format.json { render json: @product_types }
end
end
def show
@product_type = ProductType.find(params[:id])
respond_to do |format|
format.html
format.json { render json: @product_type }
end
end
def new
@product_type = ProductType.new
respond_to do |format|
format.html
format.json { render json: @product_type }
end
end
def edit
@product_type = ProductType.find(params[:id])
end
def create
@product_type = ProductType.new(product_type_params)
respond_to do |format|
if @product_type.save
format.html { redirect_to @product_type, notice: 'Product type was successfully created.' }
format.json { render json: @product_type, status: :created, location: @product_type }
else
format.html { render action: "new" }
format.json { render json: @product_type.errors, status: :unprocessable_entity }
end
end
end
def update
@product_type = ProductType.find(params[:id])
respond_to do |format|
if @product_type.update_attributes(product_type_params)
format.html { redirect_to @product_type, notice: 'Product type was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @product_type.errors, status: :unprocessable_entity }
end
end
end
def destroy
@product_type = ProductType.find(params[:id])
@product_type.destroy
respond_to do |format|
format.html { redirect_to product_types_url }
format.json { head :no_content }
end
end
def product_type_params
params.require(:product_type).permit(
:name, fields_attributes: [ :field_type, :name, :required ] )
end
end
产品控制程序.rb
class ProductsController < ApplicationController
def index
@products = Product.all
end
def show
@product = Product.find(params[:id])
end
def new
@product = Product.new(product_type_id: params[:product_type_id])
end
def edit
@product = Product.find(params[:id])
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to @product, notice: 'Product was successfully created.'
else
render action: "new"
end
end
def update
@product = Product.find(params[:id])
if @product.update_attributes(product_params)
redirect_to @product, notice: 'Product was successfully updated.'
else
render action: "edit"
end
end
def destroy
@product = Product.find(params[:id])
@product.destroy
redirect_to products_url
end
private
def product_params
params.require(:product).permit(:name, :price, :product_type_id,
:properties)
end
end
_form.html.erb
<%= 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 %>
<%= f.hidden_field :product_type_id %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<%= f.fields_for :properties, OpenStruct.new(@product.properties) do |builder| %>
<% @product.product_type.fields.each do |field| %>
<%= render "products/fields/#{field.field_type}", field: field, f: builder %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
日志
Completed 500 Internal Server Error in 9ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (undefined method `fields' for nil:NilClass):
22: </div>
23:
24: <%= f.fields_for :properties, OpenStruct.new(@product.properties) do |builder| %>
25: <% @product.product_type.fields.each do |field| %>
26: <%= render "products/fields/#{field.field_type}", field: field, f: builder %>
27: <% end %>
28: <% end %>
app/views/products/_form.html.erb:25:in `block (2 levels) in _app_views_products__form_html_erb__38604707748806799_70122829936940'
app/views/products/_form.html.erb:24:in `block in _app_views_products__form_html_erb__38604707748806799_70122829936940'
app/views/products/_form.html.erb:1:in `_app_views_products__form_html_erb__38604707748806799_70122829936940'
app/views/products/new.html.erb:3:in `_app_views_products_new_html_erb__487839569246893265_70122829438980'
railscast上有人发布了一条关于同样问题的评论,另一人回复说,这与遗漏<%= f.hidden_field :product_type_id %>
有关。我错过了什么吗?谢谢你花时间仔细研究这个很可能是愚蠢的错误。
问题在于如何在ProductsController#new
:中的此行实例化@product
def new
@product = Product.new(product_type_id: params[:product_type_id])
end
您引用的是product_type的id,而不是具体引用。由于@product未保存在您的new
操作中,因此@product.product_type
从未从数据库加载,并且始终为nil
。要修复,请加载product_type
并直接从新产品中引用它
def new
product_type = ProductType.find(params[:product_type_id])
@product = Product.new(product_type: product_type)
end