views:

17

answers:

1

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.

+2  A: 

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.

lakshmanan
I get this error: undefined method `where' for #<Class:0x102c748d8>
bgadoci
Which version of rails are you using?
nathanvda
I'm using Rails 2.3.8
bgadoci
see the modified code above
lakshmanan
that worked. Thank you.
bgadoci