-1

I have a client who runs ruby on rails and unfortunately i'm not 100% o fay with Ruby... Anyway today their site keeps returning error 500 when browsing to the site.

Looking at the error_log all I can see is the following:

[ 2015-11-16 20:16:45.5125 11424/7fcdb8049700 Pool2/SmartSpawner.h:298 ]: Preloader for /home/rails/production started on PID 22379, listening on unix:/tmp/p$
App 22504 stdout:
App 22950 stdout:
App 22950 stderr: /home/rails/.rvm/gems/ruby-1.9.3-p484/gems/tlsmail-0.0.1/lib/net/smtp.rb:806: warning: already initialized constant SMTPSession
App 22950 stderr: /home/rails/.rvm/gems/ruby-1.9.3-p484/gems/tlsmail-0.0.1/lib/net/pop.rb:687: warning: already initialized constant POP
App 22950 stderr: /home/rails/.rvm/gems/ruby-1.9.3-p484/gems/tlsmail-0.0.1/lib/net/pop.rb:688: warning: already initialized constant POPSession
App 22950 stderr: /home/rails/.rvm/gems/ruby-1.9.3-p484/gems/tlsmail-0.0.1/lib/net/pop.rb:689: warning: already initialized constant POP3Session
App 22950 stderr: /home/rails/.rvm/gems/ruby-1.9.3-p484/gems/tlsmail-0.0.1/lib/net/pop.rb:702: warning: already initialized constant APOPSession
App 22950 stdout: MultiParameterHack loaded
App 22950 stderr: [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set$
[ 2015-11-16 20:33:27.7232 11424/7fcdb9f61700 Pool2/SmartSpawner.h:298 ]: Preloader for /home/rails/production started on PID 22950, listening on unix:/tmp/p$
App 23101 stdout:

Can anyone shed any light on this for me?

I can also see this within the production.log:

Rendered shared/_meganav.html.erb (86.0ms)
Rendered pages/home.html.erb within layouts/application (197.9ms)
Completed 500 Internal Server Error in 278ms
** [Airbrake] Success: Net::HTTPOK
** [Airbrake] Environment Info: [Ruby: 1.9.3] [Rails: 3.0.19] [Env: production]
** [Airbrake] Response from Airbrake:
UUID: 1555278126118213905
URL:  https://airbrake.io/locate/1555278126118213905

ActionView::Template::Error (undefined method `has_image?' for nil:NilClass):
    269:                           <% event = Event.featured.newest.first %>
    270:
    271:              <article class="featured-event">
    272:                <% if event.has_image? %>
    273:                <div class="image"><span></span><%= link_to(image_tag(event.image.expiring_url(:featured)), event_path(event)) %></div>
    274:                <% end %>
    275:                  <div class="text">
  app/views/shared/_meganav.html.erb:272:in `_app_views_shared__meganav_html_erb__3450254428998327647_59198060__3999436880091196355'
  app/views/layouts/application.html.erb:223:in `_app_views_layouts_application_html_erb__1905251585106566541_49797400_3662579409495417543'
6
  • Apparently not a relevant section of logs. We need more data. Nov 16, 2015 at 20:53
  • Hey @KarlWilbur thanks for reply. Which logs should I take a look at?
    – WebDevB
    Nov 16, 2015 at 20:54
  • It depends on the app and configuration. Maybe log/production.log. What web server is used? The web server's error log might be helpful too. Nov 16, 2015 at 20:56
  • @KarlWilbur the errors above were from the server log /var/log/error_log
    – WebDevB
    Nov 16, 2015 at 20:58
  • I'll a look at the other one now.
    – WebDevB
    Nov 16, 2015 at 20:58

2 Answers 2

2

I suspect that your issue is that there is no Event that matches featured.newest, which means that event is nil. As nil#has_image? is not an available method, a NoMethodError is thrown and you end up seeing a 500 error on the server.

The Event query belongs in your controller, and your output should have a nil-guard to ensure this doesn't happen. For example:

# app/controllers/pages_controller.rb
def index
  @event = Event.featured.newest.first
end

# app/views/pages/home.html.erb
<% if @event.present? %>
  <article class="featured-event">
  ...
  </article>
<% end %>
3
  • Hi coreyward thanks for this man, sorry I have never used ruby... Do I just need to add <% if @event.present? %> before the <% if event.has_image?%>?
    – WebDevB
    Nov 16, 2015 at 21:29
  • Depending on your application needs, you might be able to do this in your controller: @event = Event.featured.newest.first || Event.new. Nov 16, 2015 at 21:34
  • I've made that change but still getting the same error? Do i need to recompile anything?
    – WebDevB
    Nov 16, 2015 at 21:47
1

Change <% if event.has_image? %> to:

<% if event && event.has_image? %>

...or in Ruby >= 2.3

<% if event&.has_image? %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.