【问题标题】:error while setting up an existing Ruby On Rails project to local machine将现有的 Ruby On Rails 项目设置到本地计算机时出错
【发布时间】:2014-09-30 03:41:16
【问题描述】:

在数据库设置终端命令“rails server”成功执行后,我正在为我的本地机器设置一个现有的“Ruby On Rails”项目,但是当我在浏览器中点击 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

【问题讨论】:

  • 发布您的content_controller 代码
  • 你好@Pavan 我已经添加了我的内容控制器代码请看一下...
  • Setting好像没有记录,所以报错。检查Setting中是否存在记录
  • 你跑rake db:seed了吗?
  • 是的@Pavan 我也认为没有记录但是我如何将数据导入我的数据库我不知道我是 ROR 的初学者......

标签: ruby-on-rails ruby setup-project


【解决方案1】:

问题是这一行:

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

在这里,您在 Setting.first 上调用 features 方法,您会收到错误,因为 Setting.first 返回 nil(这意味着您的 设置表 现在是空的),在这种情况下,在 Setting.first 上调用 features 方法就像调用: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")

希望有帮助

【讨论】:

    猜你喜欢
    • 2014-05-20
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多