我有两个轨道模型,关系用户有很多设计。 designs有一个paperclip的'has_attached_file'。
因此,当我为图像标记执行以下操作时,它可以正常工作。
@img_des = current_user.designs.first.img.url
但是不能让它发挥作用。
@img_des = current_user.designs.where(id: params[:id]).img.url
引发以下错误
NoMethodError in DesignerController#show
undefined method `design' for #<ActiveRecord::Relation::ActiveRecord_Relation_Design:0xb1ede48>
任何建议,主要是因为我对rails嵌套模型缺乏了解。
答案 0 :(得分:1)
你必须首先使它成为对象而不是数组,因为它返回ARRAY
@img_des = current_user.designs.where(id: params[:id]).first.img.url
答案 1 :(得分:1)
对于单个记录,最好使用find
@img_des = current_user.designs.find(params[:id]).img.url