Controller/View concerns

Hello!

I have 2 questions.

controller:

@websites = Website.find(:all)

view:

<% for website in @websites %>

<%=website.name%>

<%for photo in website.website_photos%>
<%= image_tag photo.public_filename, :size => photo.image_size %>
<%end%>

<%end%>

So, i have websites, and they have many website_photos. This code works
fine but how can i display for example 2 first photos, not loop through
all of them, without putting the logic in the view. I could only achieve
this by putting some variables in the view.

  1. My second question is about general way how to sort stuff. My first
    idea was to sort by id of the photo, or maybe make a special boolean
    field in the table and if it’s true the photo will be listed on the main
    page, otherwise not.

On Feb 9, 5:40 am, Pablo [email protected] wrote:

view:

page, otherwise not.


Posted viahttp://www.ruby-forum.com/.

you could create a helper, otherwise something like

for idx in 0…1
if @website[idx]
etc

Not sure what the other question you are asking is. You can sort:
list.sort {|x,y| x.fld <=> y.fld}

You can use .find on the collection.

website.website_photos.find :all, :limit => 2

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs