简体   繁体   English

重写_path rails helper方法

[英]Overriding _path rails helper method

There is a column in the database called photo_path in the post table and in my view file where i display all the posts i have: 在帖子表和我的视图文件中,数据库中有一列称为photo_path,其中显示了我拥有的所有帖子:

<% if post.photo_path != 0 %>

That is, if there is no photo for that particular post. 也就是说,如果该特定帖子没有照片。 But the problem is, i'm getting a conflict with the rails helper method. 但是问题是,我与rails helper方法发生冲突。 It's converting my code to 它将我的代码转换为

<%= if /uploads/post/photo_path/1/0 != 0 %>

How do i make it use the database column photo_path and not the photo_path helper method? 如何使用数据库列photo_path而不是photo_path helper方法?

!= 0 is very bad syntax, use instead ".blank?" != 0是非常糟糕的语法,请改用“ .blank?” or ".present?" 或“ .present?” methods. 方法。

Your post model have associated with photos model? 您的帖子模型与照片模型相关联? If yes, why do you need this column in db? 如果是,为什么在db中需要此列? Remove from db this column or rename it to human name. 从数据库中删除此列或将其重命名为人名。 Maybe it simpler user this? 也许更简单的用户呢? :

<% if post.photos.present? %>
.......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM