我有一个食谱应用程序,允许用户保存他们最喜欢的食谱,并在他们的个人资料页面(line_items索引),他们可以根据自己喜欢的食谱获得一个随机的每周菜单。我试图打印出每周菜单的成分,但到目前为止我无法打印出成分的名称。目前,当我尝试类似的东西时:
<%= line_item.recipe.ingredient.name %>
结果是“成分”。
我怀疑这可能与协会有关,但我无法弄明白。任何帮助将不胜感激。
订单项:
class LineItem < ActiveRecord::Base
belongs_to :user
belongs_to :recipe
end
配方:
class Recipe < ActiveRecord::Base
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
has_many :ingredients
has_many :directions
has_many :users, :through => :line_items
accepts_nested_attributes_for :ingredients,
reject_if: :all_blank,
allow_destroy: true
accepts_nested_attributes_for :directions,
reject_if: :all_blank,
allow_destroy: true
validates :title, :description, :image, presence:true
has_attached_file :image, styles: { medium: "400x400#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
private
#ensure that there are no line items referencing this recipe
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line items present')
return false
end
end
end
成分:
class Ingredient < ActiveRecord::Base
belongs_to :recipe
end
订单项视图:
<button class="btn btn-primary " type="button" data-toggle="collapse" data-target="#menu" aria-expanded="true" aria-controls="menu">
This Week's Menu
</button>
<div class="collapse in" id="menu">
<div class="well">
<br>
<% LineItem.where(id: ids_of_line_items_this_week).each do |line_item| %>
<li><%= line_item.recipe.title %> <%= link_to 'Replace', line_item_replace_weekly_line_item_path(line_item_id: line_item.id), method: :put, :class => "btn btn-default btn-xs" %>
<br>
<%= line_item.recipe.ingredients.name %> </li>
<% end %>
</div>
</div>