Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Getting the next link in the view

l have an rails app that got a list of images and the clients can view the images individually. In the view for the individual images l wanted a link that when the users click it takes them to the individual page of the next image. This for now works for almost all images except the first and last. Any ideas how l can accomplish this please?

In my view l have the following code

%br
%br
%div.row
  %div.small-10.columns
    %p.loading
      Loading Entry ...
    =image_tag(src="http://localhost:3000/assets/logo-new-light-4c847a903227606be41e87caddeabc67.png")
    %br
    %br
  %div.small-2.columns.swl
    %p
      Please give a score. <br /> 5 is highest and 0 is lowest
    = simple_form_for @vote do | f |
      = f.input :score, collection: 0..5, :include_blank => false
      = f.input :judge_id, :as => :hidden, :input_html => { :value => current_judge.id }
      = f.input :entry_id, :as => :hidden, :input_html => { :value => @entry.id }
      = f.button :submit

#related code starts here 
    %p
      = link_to 'Skip and Go Back to List', judge_path(obfuscate(@entry.previous_post));
      = link_to 'Skip and Go Back to List', judge_path(obfuscate(@entry.next_post));


%div.row.swl
  %div.small-3.columns
    %strong
      Entry Number: 
    = "#{@entry.id} <br />".html_safe
    %strong
      Size: 
    = "Height: #{@entry.height} x Width #{@entry.width} <br />".html_safe
    %strong
      Media: 
    = "#{@entry.type_of_media}"
  %div.small-9.columns
    %div.row
      %div.small-12.columns
        %p
          %strong
          Scale:
          Woman is 172cm (5 ft 8 inches) tall.
        %br
    %div.row
      %div.small-12.columns
        = image_tag("woman.png",:class => 'woman')
        %div.picture{:style => "width: #{@entry.width}px; height: #{@entry.height}px"}




:javascript
  $(function(){
    var imgLoad = imagesLoaded('body');
    imgLoad.on( 'done', function( instance ) {
      $('.loading').fadeOut(function(){
        $('.swl').fadeIn();
      });

    }); 
  });


/- @entries.values.each do | entry |
/= entry[0].media(:medium)
/- @entries.each do | entry |
/= entry

My controller have the following code

class Entry < ActiveRecord::Base

    monetize :price

    #has_attached_file :media, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
    has_attached_file :media, :styles => { :medium => "300x300>", :thumb => "100x100>", :large => "600x" }, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml", :url => "/system/:hash.:extension", :hash_secret => "longSecretStringNewLight", :s3_protocol => :https
    validates_attachment_content_type :media, :content_type => ["image/jpg", "image/jpeg", "image/png"]
    validates_attachment_size :media, :in => 0.megabytes..2.megabytes

    belongs_to :entrant

    has_many :votes
    #has_many :entrants, :through => :votes

    validates :title_of_work, presence: true
    validates :height, presence: true, numericality: true
    validates :width, presence: true, numericality: true
    validates :price, presence: true
    validates :type_of_media, presence: true
    validates :media, :presence => { message: "Please upload an image" }

    def has_not_been_paid
        self.paid != true
    end
#related code starts here
def previous_post
  self.class.where("id < ?", id).last.id
end

def next_post
  self.class.where("id > ?", id).first.id
end
end

Jim Hoskins Jason Seifer