这个令我感到困惑。我已经把头撞了几个星期了,现在无处可去。对不起,如果它很明显,我还是铁杆新手......
该应用正在对Unsplash图片服务进行API调用。
因此application_helper具有以下方法:
def show_photo(size)
@photo = Unsplash::Photo.random(query:"cars")[:urls][size.to_sym]
end
该视图具有以下内容:
<%= image_tag(show_photo('small'), height: "220", width:"220") %>
这显示很好。
问题在于我想从对象中提取其他一些方法。我已在应用程序中添加了另一种方法,如下所示:
def show_author
@photo.user.name
end
和相应的观点: 照片提供者:&lt;%= show_author%&gt;
然后我收到此错误: 未定义的方法`user&#39;对于#
然而,当在Rails控制台中调用该方法时,它可以正常工作:
@photo = Unsplash::Photo.random(query:"cars")
=> #<Unsplash::Photo:0x00000004fcf950 @attributes=#<OpenStruct id="CKeoh-
90U3E", created_at="2017 .......
2.3.0 :003 > @photo.user.name
=> "Florian Schneider"
如果要在视图中使用user.name,我需要做什么?
提前多多感谢,
鲁道夫
答案 0 :(得分:1)
@photo
不是照片,看起来您正在使用[:urls][size.to_sym]
在@photo上调用某些元数据,而您无法在.user
上调用Unsplash::Photo.random(query:"cars")[:urls][size.to_sym].user
那个元数据。
你基本上是在说def photo
@photo ||= Unsplash::Photo.random(query:"cars")
end
def resized_photo(size)
photo[:urls][size.to_sym]
end
def photo_author_name
photo.user.name
end
您可能需要执行以下操作:
@photo ||=
btw while(!(USART2->SR) & (USART_SR_TC))
正在记住API调用,因此您不会多次拨打同一个电话。