rails小错误集锦 初学者可能遇到的错误

一、项目中使用了未安装的插件,

MissingSourceFile (no such file to load -- google_chart):
  app/controllers/home_page/home_controller.rb:4

 

    通过安装插件来解决

     gem install google_chart
 


二、调用方法的对象为空
NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each):
  app/controllers/search_controller.rb:49:in `index'

 

   这个是最常见的错误,根据错误信息可以知道,调用each方法的对象为空,只要检查each前的对象即可

nil.each


三、路径错误

   项目中不能找到与输入相匹配的路径,一般是手误或路径与routs中的不匹配

ActionController::RoutingError (No route matches "/albums" with {:method=>:get}):

 
四、模板错误


  与第二个类似,也是对象为空导致的。

ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]) on line #2 of app/views/layouts/home_page/_webinfo.html.erb:
1: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2: <title><%= show_page_title(@page_title,@HomePageLink) %></title>
3: <meta name=GOOGLEBOT content="index,follow">
4: <meta name="robots" content="index,follow"> 
5: <%- unless @page_keyword==nil -%>

 


五、数据库,字段错误


   数据库中没有所用到的数据库的字段

ActiveRecord::UnknownAttributeError (unknown attribute: sell_id):
 



六、模板错误


程序中调用的click_number方法不存在,其实是数据库中没有这个字段
ActionView::TemplateError (undefined method `click_number' for #<Admin::Advertisement:0x662f218>) on line #14 of app/views/admin/manage_ad/_list_ads.html.erb:
11:       <td><%= ad.page_controller %></td>
12:       <td><%= ad.position %></td>
13:       <td><%= ad.url %></td>
14:       <td><%= ad.click_number %></td>
15:       <td><%= link_to "鍒犻櫎", {:action => :destroy, :id => ad}, :method => :delete, :confirm => "浣犵‘瀹氳鍒犻櫎杩欐潯骞垮憡涔堬紵" %></td>
16:       <td><%= link_to "缂栬緫", {:action => :edit, :id => ad} %></td>
17:     </tr>

 



七、运行错误


这也是一个常见错误,取id的值的对象为空

下面这个例子:

   控制器:

ad.user_id = Register::User.find_by_username(params[:user_username]).id
 

   当在用户表里查找用户失败时,查找的对象就为空,而这时空对象取得id值时出现下面的错误
RuntimeError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id):
  app/controllers/admin/manage_ad_controller.rb:30:in `create'
  C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
  C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'

 
八、缺少end


  这种错误一般出现在模板里,使用控制语句或循环语句时忘记了end

ActionView::TemplateError (compile error
D:/cetcit/app/views/admin/data_stats/index.erb:30: syntax error, unexpected kENSURE, expecting kEND
D:/cetcit/app/views/admin/data_stats/index.erb:32: syntax error, unexpected $end, expecting kEND) on line #30 of app/views/admin/data_stats/index.erb:
 

九、关键字错误


  显示图片的关键字错误
ActionView::TemplateError (undefined method `images' for #<ActionView::Base:0xebbb88c>) on line #4 of app/views/admin/data_stats/index.erb:
1: <%#
2: # To change this template, choose Tools | Templates
3: # and open the template in the editor.
4: %>
5: <%=javascript_include_tag "admin/data_stat/jquery","admin/data_stat/jquery.ingrid","admin/data_stat/jquery.cookie"%>
6: <%= stylesheet_link_tag 'admin/data_stat/ingrid'%>
7: <%= images 'data_stat' %>

 

十、方法未知

ActionController::UnknownAction (No action responded to select_data(data). Actions: admin_role_required, hot_tags, index, select_data, sponsor_link, and user_role_required):
  C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
  C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
  C:/Ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值