Question

I am setting up an existing 'Ruby On Rails' project to my local Machine after database setup terminal command 'rails server' executes successfully but when I hit localhost:3000 in browser I get following response....

Started GET "/" for 127.0.0.1 at 2014-04-08 16:07:01 +0530
  Processing by ContentController#index as HTML
  RvClass Load (0.1ms)  SELECT "rv_classes".* FROM "rv_classes" 
  Testimonial Load (0.2ms)  SELECT "testimonials".* FROM "testimonials" WHERE "testimonials"."approved" = 't'
  Setting Load (0.2ms)  SELECT "settings".* FROM "settings" LIMIT 1
Completed 500 Internal Server Error in 1136ms

NoMethodError (undefined method `features' for nil:NilClass):
  app/controllers/content_controller.rb:18:in `index'

content_controller.rb:

class ContentController < ApplicationController
  layout "application"
  before_filter :check_for_mobile, :except => [:inventory, :disclaimer, :preowned, :sunridge_rv_video]
  before_filter :prepare_for_mobile, :only => [:inventory, :social_media ]

  def inventory
  end

 def social_media
 end

 def index
   @tweets =  begin
                Twitter.user_timeline("sunridgeRV").slice(0, 6)
              rescue Twitter::Error
                []
              end
   @feature_products = Setting.first.features.where("product_id is not null")
   @home_page_testimonials = Testimonial.where(:approved=>true).shuffle.take(3)
   @setting = Setting.first
   @products = Product.where(special_on_homepage: true)
 end

 def disclaimer
   render :partial => 'disclaimer'
 end

 def preowned
   render :partial => 'used_guarantee'
 end

 def sunridge_rv_video
   @rv_class_brands = RvClassBrand.has_video_link
 end

 def sunridge_team
   @team_members = TeamMember.order("department_id").order("id")
 end

 def sales_home
   @setting = Setting.first
 end
end
Was it helpful?

Solution

the problem is this line :

@feature_products = Setting.first.features.where("product_id is not null")

here you call features method on Setting.first, and you get the error because Setting.first return nil (which means your settings table is empty for now), calling features method on Setting.first in this case is like calling : nil.features

some persons suggest you to fill in your database rows, but i think this is not really the way to solve this, any app should be working without any existing data

to solve the above bug you should just add a condition like this :

@feature_products = Setting.first.features.where("product_id is not null") unless Setting.first.nil?

or use try method :

@feature_products = Setting.first.try(:features).where("product_id is not null")

Hope this help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top