Ruby on rails 4 变量的输出属性返回对象类,这可能是关联问题
我有一个cookbook应用程序,允许用户保存他们最喜欢的食谱,并且在他们的个人资料页面(line_items index)上,他们可以根据他们最喜欢的食谱获得随机的每周菜单。我试图打印出每周菜单的配料,但到目前为止,我无法让它打印配料的名称。当前,当我尝试以下操作时:Ruby on rails 4 变量的输出属性返回对象类,这可能是关联问题,ruby-on-rails-4,model,associations,Ruby On Rails 4,Model,Associations,我有一个cookbook应用程序,允许用户保存他们最喜欢的食谱,并且在他们的个人资料页面(line_items index)上,他们可以根据他们最喜欢的食谱获得随机的每周菜单。我试图打印出每周菜单的配料,但到目前为止,我无法让它打印配料的名称。当前,当我尝试以下操作时: <%= line_item.recipe.ingredient.name %> 结果就是“成分” 我怀疑这可能与联想有关,但我想不出来。任何帮助都将不胜感激 行项目: class LineItem <
<%= line_item.recipe.ingredient.name %>
结果就是“成分”
我怀疑这可能与联想有关,但我想不出来。任何帮助都将不胜感激
行项目:
class LineItem < ActiveRecord::Base
belongs_to :user
belongs_to :recipe
end
class LineItem
配方:
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
类配方:行\u项
接受:配料的\u嵌套\u属性\u,
如果::全部为空,则拒绝,
允许你毁灭:对
接受以下内容的\u嵌套\u属性\u:directions,
如果::全部为空,则拒绝,
允许你毁灭:对
验证:标题、:描述、:图像、状态:true
已附加_文件:图像,样式:{medium:“400x400#”}
验证附件内容类型:图像、内容类型:/\Aimage\/.\Z/
私有的
#确保没有引用此配方的行项目
def确保任何行项目均未引用
如果第_行items.empty?
返回真值
其他的
错误。添加(:base,“存在行项目”)
返回错误
结束
结束
结束
成分:
class Ingredient < ActiveRecord::Base
belongs_to :recipe
end
class成分
行项目视图:
<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>
本周的菜单
“btn btn默认btn xs”%>
食谱是否只有一种成分?看起来不像;您需要反复阅读食谱
成分。抱歉。我忘了添加视图文件。我实际上是在反复研究这个食谱。我希望这能有所帮助。如果你在重复配方成分,那么你需要像
这样的东西,以产生类似于:#。。。你需要反复研究这些成分。块参数将是成分。然后你可以用所说的配料做任何你想做的事情。您不会真的想将枚举器的本机打印到
。