是我的最后一个问题。它现在有效:没有错误。 新问题是新单位转换不与成分ID相关联。我认为这是“只是应该工作”的构建方法?
单位转换控制器:
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
单位转换模型:
class UnitConversion < ActiveRecord::Base
belongs_to :ingredient
end
成分模型:
class Ingredient < ActiveRecord::Base
belongs_to :unit
has_many :unit_conversions
end
感谢您的帮助,我今天找到了一个粗略的学习曲线:)
编辑: 更重要的是...... new.html.erb
<h1> New Derived Unit </h1>
<% form_for([@ingredient, @unit_conversion]) do |f| %>
<% f.error_messages %>
<p>
<%= f.label :name %>
<%= f.text_field :name%>
</p>
<p>
<%= f.label :conversionToBase%>
<%= f.text_field :conversionToBase%>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<% link_to 'Back', url_for( :controller => 'ingredients', :action => 'show', :id => @ingredient)%>
答案 0 :(得分:2)
确保您的表单中有form_for [@ingredient, @unit_conversion]
。
new.html.erb
<% title "New Unit Conversion" %>
<%= render :partial => 'form' %>
<p><%= link_to "Back to List", ingredient_unit_conversions_path(@ingredient) %></p>
_form.html.erb
<% form_for [@ingredient, @unit_conversion] do |f| %>
<%= f.error_messages %>
<p>
... fields here
</p>
<p><%= f.submit "Submit" %></p>
<% end %>