添加“特色帖子”到我的博客
我正在尝试向我的 Ruby on Rails 博客添加特色帖子功能。到目前为止,我已将 featured_post
列添加到我的 post
表中,如果选中该复选框且 0<,则它会传递
1
/代码> 如果没有。
现在,我尝试通过执行以下操作来提取这些帖子:
/views/posts/index.html.erb
<% @featured_post.each do |post| %>
<%= post.title %>
<% end %>
在 posts_controller.rb 中,我在 index
操作中执行以下操作:
@featured_post = Post.all
显然,这会带来在所有帖子标题中,这不是我想要的。我假设我必须为此向控制器添加一些东西,但不确定那是什么。
I am trying to add a featured post feature to my Ruby on Rails Blog. So far I have added a featured_post
column to my post
table and it passes a 1
if the check box is selected and 0
if not.
Now I am attempting to pull out these posts by doing the following:
/views/posts/index.html.erb
<% @featured_post.each do |post| %>
<%= post.title %>
<% end %>
And in the posts_controller.rb I am doing the following in the index
action:
@featured_post = Post.all
Obviously this brings in all the post titles which is not what I want. I am assuming I have to add something to the controller to all for this but not sure what that is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的帖子模型中,
在控制器中
写入此内容并在视图中使用此内容,
现在您应该获得所有特色帖子。
In your post model, write this
write this in your controller
and in view use this,
now you should get all the featured posts.