The following code does not work due to line 4 (can't convert Foo into Array). How can fix it?
def index
@foos = Foo.all
@latest_foo = Foo.last
@remaining_foos = @foos - @latest_foo
end
Here's the reason why I'm doing this:
I have a database table with 5 foos. I want the first item in my view to be in a div titled "latest-foo". I want all t开发者_如何学Pythonhe remaining foos to go into divs whose class alternates between even and odd depending on their position in my list.
Someone asked a similar question a while back on railsforum, but the approach was a little different. I guess I can use this solution but I'd like to see if there's a better, cleaner or more efficient way.
You can use Array#pop
to do this quite easily:
@foos = Foo.all # assuming that they're sorted in the order you want
@latest_foo = @foos.pop
See: http://ruby-doc.org/core/classes/Array.html#M000227
精彩评论