我正在建立一个现有的Ruby on Rails'在数据库设置终端命令' rails server'之后投影到我的本地机器成功执行,但当我在浏览器中点击localhost:3000时,我得到以下响应....
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
答案 0 :(得分:0)
问题在于这一行:
@feature_products = Setting.first.features.where("product_id is not null")
在features
上调用Setting.first
方法,您会收到错误,因为Setting.first返回 nil (这意味着您的设置表现在是空的),在这种情况下,在Setting.first上调用功能方法就像调用:nil.features
有些人建议你填写你的数据库行,但我认为这不是解决这个问题的方法,任何应用程序应该在没有任何现有数据的情况下工作
要解决上述问题,您应该添加如下条件:
@feature_products = Setting.first.features.where("product_id is not null") unless Setting.first.nil?
或使用try方法:
@feature_products = Setting.first.try(:features).where("product_id is not null")
希望这个帮助