mercredi 8 avril 2015

Error during failsafe response: Couldn't find Track without an ID

I'm wanting to have more dynamic error messages in my custom not_found (404) template. The most common issues are people looking for Artists or Tracks that are no longer in our DB. When an artist or track isn't found, render a specific message.


I get this error:



Error during failsafe response: Couldn't find Track without an ID


I want it to not find the Track or artist in the DB but not break the app. Any idears? The problem is in my custom_404_message method.


application_controller.rb



def custom_404_message
artist = Artist.find_by_slug(params[:slug])
track = Track.find(params[:id])

if params.has_key? "artists" && artist.nil?
@message = "It looks like the artist you are looking for is not on our site."
elsif params.has_key? "tracks" && track.nil?
@message = "It looks like the song you are looking for is not on our site."
else
@message = "The page you are looking for does not exist :-("
end
@message
end


errors_controller.rb



def not_found
@message = custom_404_message
render file: "#{Rails.root}/app/views/errors/not_found.html.erb", layout: false, status: 404
end


not_found.html.erb



<div id="content">
<section id="mm-not-found">
<header id="mm-not-found-landing">
<div class="cover-image header-bg" style="background-image: url(//marmoset-music-standard.s3.amazonaws.com/assets/headers/forest-5d7e462b4e278fb7b241a57e654dca5a.jpg); background-size: cover; background-position: 50% 50%; background-repeat: no-repeat no-repeat;"></div>
<hgroup>
<div class="contain">
<div class="error-icon"><span></span></div>
<h1>Ooops! Sorry for the confusion.</h1>
</div>
</hgroup>
</header>
<article>
<div class="contain">
<h2><%= @message %></h2>
<em>If you feel like this is a problem on our end, please <a href="/contact" data-bypass="true">contact us</a>.</em>
</div>
</article>
</section>
</div>
</div>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire