完全的 Rails 新手正在尝试入门。
我有两个课程,成分和单位。有磅、加仑和几十三种单位,每种成分只有一个单位。我认为我正确设置了关联/路线。
创建新成分时,我需要要求用户设置这三个单位。
我用另一个问题来说明这一点:下拉框 - 用表单中另一个表的数据填充 - Ruby on Rails https://stackoverflow.com/questions/1663499/drop-down-box-populated-with-data-from-another-table-in-a-form-ruby-on-rails
成分型号:
class Ingredient < ActiveRecord::Base
belongs_to :unit
end
单位型号:
class Unit < ActiveRecord::Base
end
Routes:
map.resources :ingredients, :has_many => :unit_conversions
map.resources :units, :has_many => :ingredients
成分 新控制器
def new
@ingredient = Ingredient.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @ingredient }
end
end
成分新观点:
<h1>New ingredient</h1>
<% form_for(@ingredient) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :needsDefrosting %><br />
<%= f.check_box :needsDefrosting %>
</p>
<p>
<%= f.label :baseName %>
<%= f.collection_select :unit_id, @ingredient, :id, :baseName, :prompt => "Select a Base Measurement"%>
<br />
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', ingredients_path %>
错误是
NoMethodError in Ingredients#new
Showing app/views/ingredients/new.html.erb where line #16 raised:
undefined method `map' for #<Ingredient:0x3dae1c0>
Extracted source (around line #16):
13: </p>
14: <p>
15: <%= f.label :baseName %>
16: <%= f.collection_select :unit_id, @ingredient, :id, :baseName, :prompt => "Select a Base Measurement"%>
17: <br />
18: </p>
19: <p>
RAILS_ROOT: C:/Users/joan/dh
我只深入了解 RoR 大约三天,所以我怀疑这很简单!