Forum: Ruby on Rails Using "if else" in a helper

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
263762338cb0208f3412d61dcf334fa6?d=identicon&s=25 atmorell (Guest)
on 2008-12-11 16:22
(Received via mailing list)
Hello,

I am trying to move some logic from my views into a helper.  If album
with id 123 is found the view displays "found". If I replace 123 with
something that does not exists the page is just blank... Why don't I
get the "not found" message?

Any ideas?

#album view
<%= link_to_album rescue nil %>

#album helper
  def link_to_album
    if Album.find(123)
      link_to "found", @album
    else
      link_to "not found", @album
    end
  end

Best regards.
Asbjørn Morell.
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2008-12-11 16:34
(Received via mailing list)
On Thu, Dec 11, 2008 at 4:22 PM, atmorell <atmorell@gmail.com> wrote:
>
> Hello,
>
> I am trying to move some logic from my views into a helper.  If album
> with id 123 is found the view displays "found". If I replace 123 with
> something that does not exists the page is just blank... Why don't I
> get the "not found" message?
>
> Any ideas?

Fire up your console and see what happens when you try these:

Album.find(123)

Album.find_by_id(123)
This topic is locked and can not be replied to.