Rails :: Some minor view sorting

You would not believe the trouble I went to just to get the correct form of arranging ‘posts’ on the index of one of my side projects. Well, it was about 20 minutes’ trouble, but still more trouble than I would have liked!

The project is something I’m making for a friend to keep track of his DVD collection, which has the field :loaned as a boolean. What I was trying to do is make it so that there were two tables on the index page. One that listed available DVDs, and another to list those that were already loaned out and obviously their relevant information (which isn’t so important at this time).

So here’s how I managed it…. :/

def index
@loaned = Movie.find(:all, :conditions => { :loaned => true })
@not_loaned = Movie.find(:all, :conditions => { :loaned => false })
end

(Looks simple I know, but it took me at least 20 minutes to work out :| ). Then in the ‘index.html.erb’ file you will need something similar to…

....
Available to Loan
<%- for movie in @not_loaned -%>
<%=h movie.information_etc_etc %>
<%- end -%>
<br/><hr/><br/>
Loaned out
<%- for movie in @loaned -%>
<%=h movie.information_etc_etc %>
<%- end -%>
.....

And it works. Well, it should do. As a side note: the erubis tags <%- code -%> removes the double spacing in the Source View of your webpage, as opposed to the standard <% code %>. There is a RailsCasts episode that goes over this I believe. Just a tidbit for you. :p

Sorry for the realllllly bad code pasting. WordPress.com doesn’t like code pastes. :(

~ by deadbadger on April 22, 2008.

Leave a Reply