Ruby-on-rails has_many 构建方法,Rails
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1993615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
has_many build method, Rails
提问by Joan
Another newbie question.
另一个新手问题。
The goal: each ingredient can have zero or more unit conversions tied to it. I want to put a link to creating a new unit conversion on the page that shows a specific ingredient. I can't quite get it to work.
目标:每种成分都可以有零个或多个与之相关的单位转换。我想在显示特定成分的页面上放置一个创建新单位转换的链接。我不能让它工作。
Ingredient Model:
配料型号:
class Ingredient < ActiveRecord::Base
belongs_to :unit
has_many :unit_conversion
end
Unit Conversion Model:
单位换算模型:
class UnitConversion < ActiveRecord::Base
belongs_to :Ingredient
end
Unit Conversion Controller (for new)
单位转换控制器(新)
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
Relevant Routes:
相关路线:
map.resources :ingredients, :has_many => :unit_conversions
Show Ingredient Link:
显示成分链接:
<%= link_to 'Add Unit Conversion', new_ingredient_unit_conversion_path(@ingredient) %>
This is the error:
这是错误:
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'
Help! I'm all mixed up about this.
帮助!我对此很困惑。
回答by Chandra Patni
Unit Conversion Controller for newand createshould be:
单位换算控制器new和create应该是:
def new
@ingredient = Ingredient.find(params[:ingredient_id])
@unit_conversion = @ingredient.unit_conversions.build
end
def create
@ingredient = Ingredient.find(params[:ingredient_id])
@unit_conversion = @ingredient.unit_conversions.build(params[:unit_conversion])
if @unit_conversion.save
flash[:notice] = "Successfully created unit conversion."
redirect_to ingredient_unit_conversions_url(@ingredient)
else
render :action => 'new'
end
end
Also, this screencastis a nice resource for nested resources.
此外,此截屏视频是嵌套资源的好资源。
回答by nowk
has_many :unit_conversion
Should be pluralized since you're calling it with
应该是复数,因为你用
@unit_conversion = @ingredient.unit_conversions.build
your controller
你的控制器
def new
@ingredient = Ingredient.all
should be calling #newto setup a new Ingredient or #findto grab an existing Ingredient.
应该调用#new以设置新成分或#find获取现有成分。
@ingredient = Ingredient.new # returns a new Ingredient
or
或者
@ingredient = Ingredient.find(...) # returns an existing Ingredient
Which one you choose is up to your requirements.
您选择哪一种取决于您的要求。
Also, this is a typo, right?
另外,这是一个错字,对吧?
belongs_to :Ingredient
You might want to lowercase :ingredient
你可能想小写 :ingredient

