另一个新手问题.
目标:每种成分可以有零个或多个单位转换.我想在显示特定成分的页面上创建一个新的单位转换链接.我无法让它发挥作用.
成分模型:
class Ingredient < ActiveRecord::Base
belongs_to :unit
has_many :unit_conversion
end
Run Code Online (Sandbox Code Playgroud)
单位换算模型:
class UnitConversion < ActiveRecord::Base
belongs_to :Ingredient
end
Run Code Online (Sandbox Code Playgroud)
单位转换控制器(新)
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
Run Code Online (Sandbox Code Playgroud)
相关路线:
map.resources :ingredients, :has_many => :unit_conversions
Run Code Online (Sandbox Code Playgroud)
显示成分链接:
<%= link_to 'Add Unit Conversion', new_ingredient_unit_conversion_path(@ingredient) %>
Run Code Online (Sandbox Code Playgroud)
这是错误:
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'
Run Code Online (Sandbox Code Playgroud)
救命!我对这一切都很不满.
尝试入门的完整铁轨新手.
我有两个课程,成分和单元.有三个单位,磅,加仑和几十个,每个成分只有一个单位.我想我已正确设置了关联/路由.在创建新配料时,我需要用户从这三个设置单位.我用另一个问题来解决这个问题:Drop Down Box - 填充表单中另一个表的数据 - Ruby on Rails
成分模型:
class Ingredient < ActiveRecord::Base
belongs_to :unit
end
Run Code Online (Sandbox Code Playgroud)
单位模型:
class Unit < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)
路线:
map.resources :ingredients, :has_many => :unit_conversions
map.resources :units, :has_many => :ingredients
Run Code Online (Sandbox Code Playgroud)
配料新控制器
def new
@ingredient = Ingredient.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @ingredient }
end
end
Run Code Online (Sandbox Code Playgroud)
成分新视图:
<h1>New ingredient</h1>
<% form_for(@ingredient) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p> …Run Code Online (Sandbox Code Playgroud)