新手问题 undefined method `gsub' for nil:NilClass

w7938940 · 2012年11月02日 · 最后由 woaigithub 回复于 2012年11月04日 · 9350 次阅读

view: Showing /home/victor/Documents/bbs/app/views/posts/index.html.erb where line #6 raised: Extracted source (around line #6):

3: <% @posts.each do |post| %>
4:     <div class="post-line">
5:       <div class="title">
6:       <%= link_to post.title, post %>
7:       </div>
8:       <div class="info">
9:         <a href="/users/<%= post.user_id %>"><%= User.find(post.user_id).username %></a>发布于<%= post.created_at %> |

controller:

def index
  @posts = Post.order("created_at desc").paginate(page: params[:page], per_page: 10)

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @posts }
  end
end

服务器提示:ActionView::Template::Error (undefined method `gsub' for nil:NilClass):

gsub是什么东西,我的代码里也没有这个东西,google 也没有类似我的这种情况,求解答。

github 代码:https://github.com/Youngv/bbs

你最好把 log 贴出来 看看

@w7938940 首先 Showing/home/victor/Documents/bbs/app/views/posts/index.html.erb where line #6 raised: Extracted source (around line #6): 提示你第 6 行的代码有问题。 link_to 不是这么用的。 <%= link_to post.title, posts_path %> 这是我的代码,和你的代码比较一下,你会发现第二个参数不一样,在项目目录下用 rake routes 会出来路由信息。具体的可以看看 敏捷开发上关于链接的使用。

#2 楼 @tiseheaini link_to 是可以

link_to post.title, post

这么用的!

Rails 会自动转换 post 为 post_path(post.id)

仔细看了一下,也没发现问题,得帖 log 看看

此外,楼主的这一行用法不对

<a href="/users/<%= post.user_id %>"><%= User.find(post.user_id).username %></a>发布于<%= post.created_at %>

你应该

<% if !post.user.blank? %><%= link_to post.user.username, user_path(post.user_id) > 
<% end %>
发布于 <%= post.created_at %>

还有 Controller 里面的排序,用 id desc 来代替 created_at desc 达到按照创建时间倒叙排列的做法效率将会更好!

#3 楼 @huacnlee 貌似我错了,第二个参数可以是 对象,可错误信息为什么提示 第 6 行错误呢??

很奇怪的是只有 post_path 这个路由不能识别,其他的诸如 edit_post_path, posts_path 都可以识别。

@tiseheaini 借地 请教下@huacnlee 就是我有 SessionController 和 对应的 SessionHelper 为什么 controller 调用不了 helper 里面的方法

Helper 的方式是给 View 里面调用的,你不应该在 Controller 或 Model 里面使用

@huacnlee 找到原因了

#9 楼 @villins 楼主不地道,SessionHelper 里定义的方法只能在视图文件里调用,controller 里要使用的方法应该定义在 model 里面。

@huacnlee @tiseheaini 因为是登录时需要创建 session,所以就使用了。记下了

楼主的这段代码干了很多事啊,这不是挖坑害自己么?

module NavLinkHelper

def nav_link(title, path, html_options = {}, options = {}) LinkGenerator.new(request, title, path, html_options, options).to_html end

class LinkGenerator include ActionView::Helpers::UrlHelper

#4 楼 @huacnlee log 显示如下:

Started GET "/" for 127.0.0.1 at 2012-11-03 11:21:47 +0800
Connecting to database specified by database.yml
Processing by PostsController#index as HTML
  Mainboard Load (0.6ms)  SELECT `mainboards`.* FROM `mainboards` 
  Post Load (0.4ms)  SELECT `posts`.* FROM `posts` ORDER BY created_at desc LIMIT 10 OFFSET 0
  Rendered posts/index.html.erb within layouts/application (52.5ms)
Completed 500 Internal Server Error in 224ms

ActionView::Template::Error (undefined method `gsub' for nil:NilClass):
    3: <% @posts.each do |post| %>
    4:     <div class="post-line">
    5:       <div class="title">
    6:       <%= link_to post.title, post %>
    7:       </div>
    8:       <div class="info">
    9:         由<a href="/users/<%= post.user_id %>"><%= User.find(post.user_id).username %></a>发布于<%= post.created_at %> |
  app/views/posts/index.html.erb:6:in `block in _app_views_posts_index_html_erb___578724762_89298580'
  app/views/posts/index.html.erb:3:in `_app_views_posts_index_html_erb___578724762_89298580'
  app/controllers/posts_controller.rb:12:in `index'


  Rendered /home/victor/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.7/lib/action_dispatch/middleware/templates/rescues/_trace.erb (6.0ms)
  Rendered /home/victor/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered /home/victor/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.7/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (14.2ms)

#15 楼 @w7938940 我说错了,Rails 错误的时候,页面上有个详细的错误信息,把那个发出来看看

#17 楼 @w7938940 别截图啊,点开 Full Trace 看看里面的内容

@huacnlee Full Trace:

/home/victor/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/cgi/util.rb:7:in `escape'
activesupport (3.2.7) lib/active_support/core_ext/object/to_query.rb:10:in `to_query'
activesupport (3.2.7) lib/active_support/core_ext/object/to_param.rb:52:in `block in to_param'
activesupport (3.2.7) lib/active_support/core_ext/object/to_param.rb:51:in `each'
activesupport (3.2.7) lib/active_support/core_ext/object/to_param.rb:51:in `collect'
activesupport (3.2.7) lib/active_support/core_ext/object/to_param.rb:51:in `to_param'
actionpack (3.2.7) lib/action_dispatch/http/url.rb:47:in `url_for'
actionpack (3.2.7) lib/action_dispatch/routing/route_set.rb:590:in `url_for'
actionpack (3.2.7) lib/action_dispatch/routing/url_for.rb:148:in `url_for'
actionpack (3.2.7) lib/action_view/helpers/url_helper.rb:107:in `url_for'
actionpack (3.2.7) lib/action_dispatch/routing/route_set.rb:213:in `post_path'
actionpack (3.2.7) lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url'
actionpack (3.2.7) lib/action_dispatch/routing/polymorphic_routes.rb:135:in `polymorphic_path'
actionpack (3.2.7) lib/action_view/helpers/url_helper.rb:111:in `url_for'
actionpack (3.2.7) lib/action_view/helpers/url_helper.rb:242:in `link_to'
app/views/posts/index.html.erb:6:in `block in _app_views_posts_index_html_erb___207551636_84847140'
activerecord (3.2.7) lib/active_record/relation/delegation.rb:6:in `each'
activerecord (3.2.7) lib/active_record/relation/delegation.rb:6:in `each'
app/views/posts/index.html.erb:3:in `_app_views_posts_index_html_erb___207551636_84847140'
actionpack (3.2.7) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.7) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.7) lib/action_view/template.rb:143:in `render'
actionpack (3.2.7) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
actionpack (3.2.7) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.7) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.7) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.7) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.7) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
actionpack (3.2.7) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
actionpack (3.2.7) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
actionpack (3.2.7) lib/action_view/renderer/template_renderer.rb:18:in `render'
actionpack (3.2.7) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.2.7) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.2.7) lib/abstract_controller/rendering.rb:110:in `_render_template'
actionpack (3.2.7) lib/action_controller/metal/streaming.rb:225:in `_render_template'
actionpack (3.2.7) lib/abstract_controller/rendering.rb:103:in `render_to_body'
actionpack (3.2.7) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
actionpack (3.2.7) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
actionpack (3.2.7) lib/abstract_controller/rendering.rb:88:in `render'
actionpack (3.2.7) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.2.7) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.2.7) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/home/victor/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
activesupport (3.2.7) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.2.7) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.2.7) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
activerecord (3.2.7) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.2.7) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.2.7) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.2.7) lib/action_controller/metal/mime_responds.rb:196:in `respond_to'
app/controllers/posts_controller.rb:12:in `index'
actionpack (3.2.7) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.7) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.7) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.7) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.7) lib/active_support/callbacks.rb:436:in `_run__881899361__process_action__894307140__callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.7) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.7) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.7) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.7) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.7) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.7) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.7) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.7) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.7) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.7) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.7) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.7) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.7) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.7) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.7) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.7) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.7) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.7) lib/action_dispatch/routing/route_set.rb:600:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.7) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.7) lib/active_support/callbacks.rb:405:in `_run__821701562__call__524177637__callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.7) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.7) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.7) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.7) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.7) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.7) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.7) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.7) lib/rails/engine.rb:479:in `call'
railties (3.2.7) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.7) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/home/victor/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/home/victor/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/home/victor/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

nav_link_helper.rb 里面的东西干掉!我靠,搞这么复杂的东西干嘛,你知道这些东西的作用么? 问题应该在于,干掉以后试试看行不行!

def comparable_path_for(path)
  if @options[:ignore_params]
    path.gsub(/\?.*/, '')
  else
    path
  end
end

#20 楼 @huacnlee 去掉之后还是不行,还是这个问题

#20 楼 @huacnlee #21 楼 @w7938940 如果我遇到误删这种问题,我会用 git 看看,我删了哪些东西,改了哪些。 楼主用 git 新建一个分支,然后再新建的分支上回退,看看问题还有不,如果有就再回退,一直到回退到问题没有,然后看看两次提交之间误删了哪些东西

#20 楼 @huacnlee #22 楼 @tiseheaini 我把 routes.rb 里面的一句 root :to => 'posts#index', as: 'post' 换做 root :to => 'posts#index' 就正常了,这是什么原因?

#23 楼 @w7938940 posts#index 的路由应该是 posts,post 就跟 show 方法重了

#23 楼 @w7938940 4.9 Using :as in Nested Resources

The :as option overrides the automatically-generated name for the resource in nested route helpers. For example, resources :magazines do resources :ads, :as => 'periodical_ads' end

This will create routing helpers such as magazine_periodical_ads_url and edit_magazine_periodical_ad_path.

需要 登录 后方可回复, 如果你还没有账号请 注册新账号