开发者

Adding "Featured Posts" to my blog

开发者 https://www.devze.com 2023-01-20 01:12 出处:网络
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开发者_StackOverflow社区 check box is selected

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开发者_StackOverflow社区 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.


In your post model, write this

named_scope :featured,:conditions => {:featured_post => true }

write this in your controller

@featured_posts = Post.featured

and in view use this,

<% @featured_posts.each do |post| %>
    <%= post.title %>
  <% end %>

now you should get all the featured posts.

0

精彩评论

暂无评论...
验证码 换一张
取 消