另一个新手问题。has_many构建方法,Rails
目标:每种成分可以有零个或多个与之相关的单位转换。我想要在页面上放置一个链接来创建新的单位转换,以显示特定的成分。我无法完成它的工作。
成分型号:
class Ingredient < ActiveRecord::Base
belongs_to :unit
has_many :unit_conversion
end
单位转换型号:
class UnitConversion < ActiveRecord::Base
belongs_to :Ingredient
end
单位转换控制器(新)
def new
@ingredient = Ingredient.all
@unit_conversion = @ingredient.unit_conversions.build(params[:unit_conversion])
if @unit_conversion.save then
redirect_to ingredient_unit_conversion_url(@ingredient, @comment)
else
render :action => "new"
end
end
相关途径:
map.resources :ingredients, :has_many => :unit_conversions
展会成份链接:
<%= link_to 'Add Unit Conversion', new_ingredient_unit_conversion_path(@ingredient) %>
这是错误:
NoMethodError in Unit conversionsController#new
undefined method `unit_conversions' for #<Array:0x3fdf920>
RAILS_ROOT: C:/Users/joan/dh
Application Trace | Framework Trace | Full Trace
C:/Users/joan/dh/app/controllers/unit_conversions_controller.rb:14:in `new'
帮助!我都混淆了这一点。为new
和create