• 我觉得着算是 Rails 的一个 glitch,应该在往 column_names 里面塞字符串的时候就抛出异常。我以前在国外网站看了很多帖子,也没找到答案

  • 楼上正解,受教了!

  • #34 楼 @vincent 可是我在 console 里调用过以上方法,完全没有异常抛出。

    /Users/simonykq/.rvm/rubies/ruby-1.9.3-p327/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/simonykq/rails_projects/capak-app/script/rails console
    Loading development environment (Rails 3.2.13)
    Switch to inspect mode.
    >> Place.column_names << "owner"
    Creating scope :page. Overwriting existing method Place.page.
    ["id", "name", "user_id", "latitude", "longitude", "description", "created_at", "updated_at", "address", "owner"]
    
  • activerecord (3.2.13) lib/active_record/attribute_methods/time_zone_conversion.rb:60:in `create_time_zone_conversion_attribute?'
    activerecord (3.2.13) lib/active_record/attribute_methods/time_zone_conversion.rb:24:in `attribute_cast_code'
    activerecord (3.2.13) lib/active_record/attribute_methods/serialization.rb:80:in `attribute_cast_code'
    activerecord (3.2.13) lib/active_record/attribute_methods/read.rb:72:in `define_method_attribute'
    activerecord (3.2.13) lib/active_record/attribute_methods/primary_key.rb:29:in `define_method_attribute'
    activemodel (3.2.13) lib/active_model/attribute_methods.rb:267:in `block in define_attribute_method'
    activemodel (3.2.13) lib/active_model/attribute_methods.rb:260:in `each'
    activemodel (3.2.13) lib/active_model/attribute_methods.rb:260:in `define_attribute_method'
    activemodel (3.2.13) lib/active_model/attribute_methods.rb:256:in `block in define_attribute_methods'
    activemodel (3.2.13) lib/active_model/attribute_methods.rb:256:in `each'
    activemodel (3.2.13) lib/active_model/attribute_methods.rb:256:in `define_attribute_methods'
    activerecord (3.2.13) lib/active_record/attribute_methods.rb:66:in `block in define_attribute_methods'
    <internal:prelude>:10:in `synchronize'
    activerecord (3.2.13) lib/active_record/attribute_methods.rb:63:in `define_attribute_methods'
    activerecord (3.2.13) lib/active_record/attribute_methods.rb:168:in `respond_to?'
    activesupport (3.2.13) lib/active_support/callbacks.rb:398:in `__run_callback'
    activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_find_callbacks'
    activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
    activerecord (3.2.13) lib/active_record/base.rb:523:in `init_with'
    activerecord (3.2.13) lib/active_record/inheritance.rb:68:in `instantiate'
    activerecord (3.2.13) lib/active_record/querying.rb:38:in `block (2 levels) in find_by_sql'
    activerecord (3.2.13) lib/active_record/querying.rb:38:in `collect!'
    activerecord (3.2.13) lib/active_record/querying.rb:38:in `block in find_by_sql'
    activerecord (3.2.13) lib/active_record/explain.rb:41:in `logging_query_plan'
    activerecord (3.2.13) lib/active_record/querying.rb:37:in `find_by_sql'
    activerecord (3.2.13) lib/active_record/relation.rb:171:in `exec_queries'
    activerecord (3.2.13) lib/active_record/relation.rb:160:in `block in to_a'
    activerecord (3.2.13) lib/active_record/explain.rb:34:in `logging_query_plan'
    activerecord (3.2.13) lib/active_record/relation.rb:159:in `to_a'
    will_paginate (3.0.4) lib/will_paginate/active_record.rb:127:in `block in to_a'
    will_paginate (3.0.4) lib/will_paginate/collection.rb:96:in `create'
    will_paginate (3.0.4) lib/will_paginate/active_record.rb:126:in `to_a'
    activerecord (3.2.13) lib/active_record/relation/delegation.rb:8:in `rescue in to_ary'
    activerecord (3.2.13) lib/active_record/relation/delegation.rb:6:in `to_ary'
    actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:324:in `collection_from_object'
    actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:291:in `setup'
    actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:221:in `render'
    actionpack (3.2.13) lib/action_view/renderer/renderer.rb:41:in `render_partial'
    actionpack (3.2.13) lib/action_view/helpers/rendering_helper.rb:27:in `render'
    haml (4.0.2) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
    haml (4.0.2) lib/haml/helpers.rb:89:in `non_haml'
    haml (4.0.2) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'
    app/views/home/_places.html.haml:14:in `_app_views_home__places_html_haml___844257875286865780_70322442856980'
    actionpack (3.2.13) lib/action_view/template.rb:145:in `block in render'
    activesupport (3.2.13) lib/active_support/notifications.rb:125:in `instrument'
    actionpack (3.2.13) lib/action_view/template.rb:143:in `render'
    actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:265:in `render_partial'
    actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:238:in `block in render'
    actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
    activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
    activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
    actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
    actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:237:in `render'
    actionpack (3.2.13) lib/action_view/renderer/renderer.rb:41:in `render_partial'
    actionpack (3.2.13) lib/action_view/helpers/rendering_helper.rb:27:in `render'
    haml (4.0.2) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
    haml (4.0.2) lib/haml/helpers.rb:89:in `non_haml'
    haml (4.0.2) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'
    app/views/home/index.html.haml:30:in `block in _app_views_home_index_html_haml___442512620308274113_70322440503740'
    haml (4.0.2) lib/haml/helpers/action_view_mods.rb:125:in `call'
    haml (4.0.2) lib/haml/helpers/action_view_mods.rb:125:in `block (2 levels) in form_tag_with_haml'
    haml (4.0.2) lib/haml/helpers.rb:278:in `with_tabs'
    haml (4.0.2) lib/haml/helpers/action_view_mods.rb:125:in `block in form_tag_with_haml'
    haml (4.0.2) lib/haml/helpers.rb:626:in `call'
    haml (4.0.2) lib/haml/helpers.rb:626:in `block in haml_bind_proc'
    haml (4.0.2) lib/haml/helpers/action_view_mods.rb:45:in `block in capture_with_haml'
    haml (4.0.2) lib/haml/helpers.rb:368:in `call'
    haml (4.0.2) lib/haml/helpers.rb:368:in `block in capture_haml'
    haml (4.0.2) lib/haml/helpers.rb:603:in `with_haml_buffer'
    haml (4.0.2) lib/haml/helpers.rb:364:in `capture_haml'
    haml (4.0.2) lib/haml/helpers/xss_mods.rb:61:in `capture_haml_with_haml_xss'
    haml (4.0.2) lib/haml/helpers/action_view_mods.rb:45:in `capture_with_haml'
    actionpack (3.2.13) lib/action_view/helpers/form_tag_helper.rb:663:in `form_tag_in_block'
    actionpack (3.2.13) lib/action_view/helpers/form_tag_helper.rb:70:in `form_tag'
    haml (4.0.2) lib/haml/helpers/action_view_mods.rb:128:in `form_tag_with_haml'
    haml (4.0.2) lib/haml/helpers/action_view_xss_mods.rb:18:in `form_tag_with_haml_xss'
    app/views/home/index.html.haml:21:in `_app_views_home_index_html_haml___442512620308274113_70322440503740'
    actionpack (3.2.13) lib/action_view/template.rb:145:in `block in render'
    activesupport (3.2.13) lib/active_support/notifications.rb:125:in `instrument'
    actionpack (3.2.13) lib/action_view/template.rb:143:in `render'
    actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
    actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
    activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
    activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
    actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
    actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
    actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
    actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
    actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:18:in `render'
    actionpack (3.2.13) lib/action_view/renderer/renderer.rb:36:in `render_template'
    actionpack (3.2.13) lib/action_view/renderer/renderer.rb:17:in `render'
    actionpack (3.2.13) lib/abstract_controller/rendering.rb:110:in `_render_template'
    actionpack (3.2.13) lib/action_controller/metal/streaming.rb:225:in `_render_template'
    actionpack (3.2.13) lib/abstract_controller/rendering.rb:103:in `render_to_body'
    actionpack (3.2.13) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
    actionpack (3.2.13) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
    actionpack (3.2.13) lib/abstract_controller/rendering.rb:88:in `render'
    actionpack (3.2.13) lib/action_controller/metal/rendering.rb:16:in `render'
    actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
    /Users/simonykq/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
    activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `ms'
    actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
    actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
    activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
    actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:39:in `render'
    actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
    actionpack (3.2.13) lib/action_controller/metal/mime_responds.rb:196:in `respond_to'
    app/controllers/home_controller.rb:6:in `index'
    actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action'
    actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action'
    actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    activesupport (3.2.13) lib/active_support/callbacks.rb:414:in `_run__3002800960194275602__process_action__4375595017513312296__callbacks'
    activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
    activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
    activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
    actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
    actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
    actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
    activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
    activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
    actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
    actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
    activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
    actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
    actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
    actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
    actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
    actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
    actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
    actionpack (3.2.13) 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.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
    omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!'
    omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call'
    omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!'
    omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call'
    warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
    warden (1.2.1) lib/warden/manager.rb:34:in `catch'
    warden (1.2.1) lib/warden/manager.rb:34:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
    rack (1.4.5) lib/rack/etag.rb:23:in `call'
    rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
    rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
    rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
    activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
    activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
    activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__3735062755261734804__call__1343977273893472787__callbacks'
    activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
    activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
    activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
    actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
    railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
    railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
    activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
    railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
    rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
    rack (1.4.5) lib/rack/runtime.rb:17:in `call'
    activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
    rack (1.4.5) lib/rack/lock.rb:15:in `call'
    actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
    railties (3.2.13) lib/rails/engine.rb:479:in `call'
    railties (3.2.13) lib/rails/application.rb:223:in `call'
    rack (1.4.5) lib/rack/content_length.rb:14:in `call'
    railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
    thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
    thin (1.5.1) lib/thin/connection.rb:79:in `catch'
    thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
    thin (1.5.1) lib/thin/connection.rb:54:in `process'
    thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
    eventmachine (1.0.3) lib/eventmachine.rb:187:in `run_machine'
    eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
    thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
    thin (1.5.1) lib/thin/server.rb:159:in `start'
    rack (1.4.5) lib/rack/handler/thin.rb:13:in `run'
    rack (1.4.5) lib/rack/server.rb:268:in `start'
    railties (3.2.13) lib/rails/commands/server.rb:70:in `start'
    railties (3.2.13) lib/rails/commands.rb:55:in `block in <top (required)>'
    railties (3.2.13) lib/rails/commands.rb:50:in `tap'
    railties (3.2.13) lib/rails/commands.rb:50:in `<top (required)>'
    script/rails:6:in `require'
    script/rails:6:in `<top (required)>'
    -e:1:in `load'
    -e:1:in `<main>'
    

    Full stack

  • #29 楼 @Rei place 表结构很简单

    create_table "places", :force => true do |t|
      t.string   "name"
      t.integer  "user_id"
      t.float    "latitude"
      t.float    "longitude"
      t.text     "description"
      t.datetime "created_at",  :null => false
      t.datetime "updated_at",  :null => false
      t.string   "address"
    end
    

    就这么简单

  • #28 楼 @vincent << 应该不是破坏性 ruby 方法,应该不会对原有的直进行修改吧

  • = hidden_field_tag :sort, params[:sort]
    = hidden_field_tag :direction, params[:direction]
    
    -unless @places.empty?
      %table.table
        %thead
          %tr
            %th= sortable "name"
            %th= sortable "owner"
            %th Description
            %th= sortable "address"
            %th Slots
        %tbody
          = render @places
    -else
      %br
      %br
      %strong No records found
    = will_paginate @places, class: "apple_pagination"
    

    这是 places 局部模版

  • 我这个排版怎么不好用

  • def index
      @places= Place.search(params[:search]).sort(sort_column,sort_direction).paginate(:per_page => 5, :page => params[:page])
      respond_to do |format|
        format.html
        format.js
      end
    end
    

    这是我 HomeController 里面的 index 方法

  • #18 楼 @Rei 我新手啊,刚注册的号。没用过呀

  • private
    
    def sort_column
      #%w(name owner description address).include?(params[:sort]) ? params[:sort] : "name"
      (Place.column_names << "owner").include?(params[:sort]) ? params[:sort] : "name"
    end
    
    def sort_direction
      %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
    end
    

    这是我 Home_Controller 里面的一个私有方法,我把他们都设成了 helper_method,可以在 View 里面调用。问题貌似就是在 sort_column 方法里。当我用上面的代码时好用,用下面的 column_names 时就抛出异常

  • ActionView::Template::Error (undefined method `type' for nil:NilClass):
        11:         %th= sortable "address"
        12:         %th Slots
        13:     %tbody
        14:       = render @places
        15: -else
        16:   %br
        17:   %br
      app/views/home/_places.html.haml:14:in `_app_views_home__places_html_haml___844257875286865780_70322442856980'
      app/views/home/index.html.haml:30:in `block in _app_views_home_index_html_haml___442512620308274113_70322440503740'
      app/views/home/index.html.haml:21:in `_app_views_home_index_html_haml___442512620308274113_70322440503740'
      app/controllers/home_controller.rb:6:in `index'
    
    
      Rendered /Users/simonykq/.rvm/gems/ruby-1.9.3-p327@capak/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.9ms)
      Rendered /Users/simonykq/.rvm/gems/ruby-1.9.3-p327@capak/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
      Rendered /Users/simonykq/.rvm/gems/ruby-1.9.3-p327@capak/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (12.0ms)
    
  • 我再传一张

  • 上面的是我一个 MySQL 项目抛出异常的完整异常信息。我曾经用过一个 gem debug 过异常。好像是在 Controller 的 helper method 里面调用某些 model 的 ActiveRecord 查询时抛出的。

  • #5 楼 @zj0713001 我有一个 MySQL 项目,还有一个 Postgis 项目。貌似两者都犯过这个问题

  • #3 楼 @windwiny 不是,问题时我根本就没调用什么 type 方法。不知道是谁在调用。有的时候重启一下服务器自己就好了。有的时候就不好使

  • 也没有什么线索。我碰到过很多次。随时随地什么情况下都有可能发生。纠结了很久了。但是我发现很多情况下是我在 Controller 里面写了 helper_method 类方法的时候发生的异常

  • 感觉你的问题是在 Homework Model 上。在 attr_accessible 后还需要加上 ":attachments_attributes"

  • has_many 求助 at 2013年04月11日

    @user.cards.select("cards.* carding.*").joins("INNER JOIN cardings on cardings.card_id = cards.id").where("cardings.start_date = ?", "YOUR DATE")

  • 三层关联的 form_tag 怎么写 at 2013年04月11日

    可以直接在 form_tag 底下写 fields_for.例如 form_tag :company do = fields_for :departments do |d| = d.fields_for :group do |g|