• 给新手一点学习建议 at 2012年08月08日

    #1 楼 @zw963 谢谢!

  • #2 楼 @evan 刚才换了一个名字好了 为什么这样?默认的就是|blog| ,而且之前都好的。

  • 之前没有添加 gem rails-timeago之前都正常的,实在不知道为什么在哪儿了,搜也搜不出来类似的问题,看来我又要重做了。

  • Started GET "/" for 127.0.0.1 at 2012-08-08 12:50:38 +0800
    Processing by HomeController#index as HTML
      Blog Load (0.1ms)  SELECT "blogs".* FROM "blogs" 
      Rendered home/index.html.slim within layouts/application (45.4ms)
    Completed 500 Internal Server Error in 177ms
    
    ActionView::Template::Error (`@@blogs.each do |blog|' is not allowed as an instance variable name):
        1: #main
        2:   h2 This is just a Blank Homepage!
        3:   ul.article-list
        4:     - @blogs.each do |blog|
        5:       li
        6:         article
        7:           header
      app/views/home/index.html.slim:4:in `_app_views_home_index_html_slim___808974069374869356_70347745410000'
    
    
      Rendered /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb (6.3ms)
      Rendered /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
      Rendered /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (52.0ms)
    
  • = form_for(@blog) do |f|
      - if @blog.errors.any?
        #error_explanation
          h2 = pluralize(@blog.errors.count,"errors")
          ul
            - @blog.errors.full_messages.each do |msg|
              li = msg
    
      .field
        label for="title" Title
        br
        = f.text_field :title
      .field
        label for="content" Content
        br
        = f.text_area :content
    
      .actions
        input type="submit" value="POST IT!"
    
    

    这样 ok 了,还有换成f.text_field :title这样前面加上=号就好了 = f.text_field :title

  • 好的 谢谢你

  •       h2 = pluralize(@blog.errors.count,"error")
          ul
            - @blog.errors.full_messages.each do |msg|
              li 
                = msg```
    
    这里弄错了,是一个` - @blog.errors.full_messages.each do |msg|`
    输出正常,但是还是提交报错
    
  • 很好的 vim 套件 at 2012年08月07日

    #5 楼 @sectic vico 很好 很好!太好了

  • #10 楼 @bony ??????

  • 贵在坚持!贵在执行!加油!

  • 好的 谢谢

  • #8 楼 @JeskTop 谢谢

  • #2 楼 @xds2000 谢谢

  • 我这样写:

    <% (Product.all).each do |product| %>
      <%= link_to product.name %>
    <% end %>
    

    这样可以的,我把@products直接用 product.all 代替,但是这应该是很不科学的吧?

  • 先收藏了,马上要用了,哈哈!

  • 我搜到几个了,先试试,估计有帮助的。

  • => Booting WEBrick
    => Rails 3.2.6 application starting in development on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server
    [2012-08-05 19:01:35] INFO  WEBrick 1.3.1
    [2012-08-05 19:01:35] INFO  ruby 1.9.3 (2012-04-20) [x86_64-darwin11.4.0]
    [2012-08-05 19:01:35] INFO  WEBrick::HTTPServer#start: pid=35806 port=3000
    /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/processing/rmagick.rb:248: [BUG] Segmentation fault
    ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
    
    -- Control frame information -----------------------------------------------
    c:0105 p:---- s:0545 b:0545 l:000544 d:000544 CFUNC  :read
    c:0104 p:0054 s:0541 b:0541 l:000540 d:000540 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/processing/rmagick.rb:248
    c:0103 p:0011 s:0531 b:0531 l:0019c8 d:0019c8 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/processing/rmagick.rb:161
    c:0102 p:0092 s:0526 b:0526 l:000515 d:000525 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/uploader/processing.rb:85
    c:0101 p:---- s:0521 b:0521 l:000520 d:000520 FINISH
    c:0100 p:---- s:0519 b:0519 l:000518 d:000518 CFUNC  :each
    c:0099 p:0037 s:0516 b:0516 l:000515 d:000515 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/uploader/processing.rb:81
    c:0098 p:0015 s:0512 b:0512 l:000503 d:000511 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/uploader/callbacks.rb:18
    c:0097 p:---- s:0509 b:0509 l:000508 d:000508 FINISH
    c:0096 p:---- s:0507 b:0507 l:000506 d:000506 CFUNC  :each
    c:0095 p:0059 s:0504 b:0504 l:000503 d:000503 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/uploader/callbacks.rb:18
    c:0094 p:0091 s:0499 b:0499 l:000498 d:000498 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/uploader/cache.rb:112
    c:0093 p:0019 s:0495 b:0495 l:000494 d:000494 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/mount.rb:315
    c:0092 p:0021 s:0490 b:0490 l:000489 d:000489 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/mount.rb:173
    c:0091 p:0054 s:0486 b:0486 l:000485 d:000485 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/orm/activerecord.rb:37
    c:0090 p:0106 s:0481 b:0481 l:000471 d:000480 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/attribute_assignment.rb:85
    c:0089 p:---- s:0477 b:0477 l:000476 d:000476 FINISH
    c:0088 p:---- s:0475 b:0475 l:000474 d:000474 CFUNC  :each
    c:0087 p:0089 s:0472 b:0472 l:000471 d:000471 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/attribute_assignment.rb:78
    c:0086 p:0015 s:0464 b:0464 l:0025f8 d:000463 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/persistence.rb:212
    c:0085 p:0015 s:0462 b:0462 l:0026b0 d:000461 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/transactions.rb:295
    c:0084 p:0232 s:0460 b:0460 l:000459 d:000459 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statemen
    c:0083 p:0025 s:0451 b:0451 l:000450 d:000450 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/transactions.rb:208
    c:0082 p:0022 s:0446 b:0446 l:0026b0 d:0026b0 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/transactions.rb:293
    c:0081 p:0015 s:0442 b:0442 l:0025f8 d:0025f8 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/persistence.rb:211
    c:0080 p:0022 s:0437 b:0437 l:002058 d:000436 BLOCK  /Users/yuwen/Documents/bitbucket/daci/app/controllers/articles_controller.rb:64
    c:0079 p:---- s:0434 b:0434 l:000433 d:000433 FINISH
    c:0078 p:---- s:0432 b:0432 l:000431 d:000431 CFUNC  :call
    c:0077 p:0068 s:0428 b:0428 l:000427 d:000427 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal/mime_responds.rb:270
    c:0076 p:0053 s:0421 b:0421 l:000420 d:000420 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal/mime_responds.rb:194
    c:0075 p:0040 s:0414 b:0414 l:002058 d:002058 METHOD /Users/yuwen/Documents/bitbucket/daci/app/controllers/articles_controller.rb:63
    c:0074 p:0014 s:0411 b:0411 l:000410 d:000410 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal/implicit_render.rb:4
    c:0073 p:0015 s:0405 b:0405 l:000404 d:000404 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/abstract_controller/base.rb:167
    c:0072 p:0041 s:0400 b:0400 l:000399 d:000399 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal/rendering.rb:10
    c:0071 p:0010 s:0396 b:0396 l:001f60 d:000395 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/abstract_controller/callbacks.rb:18
    c:0070 p:0140 s:0394 b:0394 l:000393 d:000393 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:425
    c:0069 p:0115 s:0388 b:0388 l:000387 d:000387 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405
    c:0068 p:0027 s:0379 b:0379 l:000378 d:000378 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385
    c:0067 p:0024 s:0374 b:0374 l:000373 d:000373 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81
    c:0066 p:0020 s:0368 b:0368 l:001f60 d:001f60 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/abstract_controller/callbacks.rb:17
    c:0065 p:0012 s:0364 b:0364 l:000363 d:000363 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal/rescue.rb:29
    c:0064 p:0010 s:0359 b:0359 l:000340 d:000358 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:30
    c:0063 p:0017 s:0355 b:0355 l:000345 d:000354 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123
    c:0062 p:0032 s:0353 b:0353 l:000352 d:000352 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20
    c:0061 p:0036 s:0346 b:0346 l:000345 d:000345 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123
    c:0060 p:0152 s:0341 b:0341 l:000340 d:000340 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:29
    c:0059 p:0143 s:0336 b:0336 l:000335 d:000335 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal/params_wrapper.rb:206
    c:0058 p:0032 s:0330 b:0330 l:000329 d:000329 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/railties/controller_runtime.rb:18
    c:0057 p:0093 s:0325 b:0325 l:000324 d:000324 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/abstract_controller/base.rb:121
    c:0056 p:0084 s:0319 b:0319 l:000318 d:000318 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/abstract_controller/rendering.rb:45
    c:0055 p:0048 s:0314 b:0314 l:000313 d:000313 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal.rb:203
    c:0054 p:0086 s:0309 b:0309 l:000308 d:000308 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal/rack_delegation.rb:14
    c:0053 p:0030 s:0303 b:0303 l:001808 d:000302 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_controller/metal.rb:246
    c:0052 p:---- s:0300 b:0300 l:000299 d:000299 FINISH
    c:0051 p:---- s:0298 b:0298 l:000297 d:000297 CFUNC  :call
    c:0050 p:0022 s:0294 b:0294 l:000293 d:000293 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/routing/route_set.rb:73
    c:0049 p:0094 s:0288 b:0288 l:000287 d:000287 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/routing/route_set.rb:36
    c:0048 p:0184 s:0282 b:0282 l:000265 d:000281 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/journey-1.0.4/lib/journey/router.rb:68
    c:0047 p:---- s:0271 b:0271 l:000270 d:000270 FINISH
    c:0046 p:---- s:0269 b:0269 l:000268 d:000268 CFUNC  :each
    c:0045 p:0051 s:0266 b:0266 l:000265 d:000265 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/journey-1.0.4/lib/journey/router.rb:56
    c:0044 p:0025 s:0262 b:0262 l:000261 d:000261 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/routing/route_set.rb:600
    c:0043 p:0014 s:0258 b:0258 l:000249 d:000257 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/warden-1.2.1/lib/warden/manager.rb:35
    c:0042 p:---- s:0256 b:0256 l:000255 d:000255 FINISH
    c:0041 p:---- s:0254 b:0254 l:000253 d:000253 CFUNC  :catch
    c:0040 p:0086 s:0250 b:0250 l:000249 d:000249 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/warden-1.2.1/lib/warden/manager.rb:34
    c:0039 p:0015 s:0245 b:0245 l:000244 d:000244 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/best_standards_support.rb:17
    c:0038 p:0015 s:0238 b:0238 l:000237 d:000237 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/etag.rb:23
    c:0037 p:0052 s:0230 b:0230 l:000229 d:000229 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/conditionalget.rb:35
    c:0036 p:0093 s:0223 b:0223 l:000222 d:000222 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/head.rb:14
    c:0035 p:0046 s:0216 b:0216 l:000215 d:000215 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/params_parser.rb:21
    c:0034 p:0054 s:0211 b:0211 l:000210 d:000210 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/flash.rb:242
    c:0033 p:0031 s:0203 b:0203 l:000202 d:000202 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205
    c:0032 p:0013 s:0195 b:0195 l:000194 d:000194 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200
    c:0031 p:0020 s:0191 b:0191 l:000190 d:000190 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/cookies.rb:338
    c:0030 p:0064 s:0183 b:0183 l:000182 d:000182 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/query_cache.rb:64
    c:0029 p:0029 s:0174 b:0174 l:000173 d:000173 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/connection_pool.r
    c:0028 p:0014 s:0166 b:0166 l:001170 d:000165 BLOCK  /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/callbacks.rb:28
    c:0027 p:0040 s:0164 b:0164 l:000163 d:000163 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405
    c:0026 p:0115 s:0157 b:0157 l:000156 d:000156 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405
    c:0025 p:0027 s:0148 b:0148 l:000147 d:000147 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385
    c:0024 p:0024 s:0143 b:0143 l:000142 d:000142 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81
    c:0023 p:0013 s:0137 b:0137 l:001170 d:001170 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/callbacks.rb:27
    c:0022 p:0039 s:0133 b:0133 l:000132 d:000132 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/reloader.rb:65
    c:0021 p:0044 s:0128 b:0128 l:000127 d:000127 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/remote_ip.rb:31
    c:0020 p:0017 s:0124 b:0124 l:000123 d:000123 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/debug_exceptions.rb:16
    c:0019 p:0017 s:0117 b:0117 l:000116 d:000116 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/show_exceptions.rb:56
    c:0018 p:0121 s:0111 b:0111 l:000110 d:000110 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/rack/logger.rb:26
    c:0017 p:0052 s:0105 b:0105 l:000104 d:000104 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/rack/logger.rb:16
    c:0016 p:0048 s:0101 b:0101 l:000100 d:000100 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/request_id.rb:22
    c:0015 p:0095 s:0094 b:0094 l:000093 d:000093 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/methodoverride.rb:21
    c:0014 p:0032 s:0089 b:0089 l:000088 d:000088 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/runtime.rb:17
    c:0013 p:0057 s:0080 b:0080 l:000079 d:000079 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/cache/strategy/local_cache.rb:72
    c:0012 p:0068 s:0076 b:0076 l:000075 d:000075 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/lock.rb:15
    c:0011 p:0124 s:0070 b:0070 l:000069 d:000069 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/middleware/static.rb:62
    c:0010 p:0032 s:0064 b:0064 l:000063 d:000063 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/engine.rb:479
    c:0009 p:0034 s:0060 b:0060 l:000059 d:000059 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application.rb:220
    c:0008 p:0015 s:0056 b:0056 l:000055 d:000055 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/content_length.rb:14
    c:0007 p:0015 s:0047 b:0047 l:000046 d:000046 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/rack/log_tailer.rb:17
    c:0006 p:0356 s:0042 b:0042 l:000041 d:000041 METHOD /Users/yuwen/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59
    c:0005 p:0257 s:0030 b:0030 l:000029 d:000029 METHOD /Users/yuwen/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138
    c:0004 p:0393 s:0020 b:0020 l:000019 d:000019 METHOD /Users/yuwen/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94
    c:0003 p:0126 s:0009 b:0009 l:0008d8 d:000008 BLOCK  /Users/yuwen/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191
    c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
    c:0001 p:---- s:0002 b:0002 l:000001 d:000001 TOP   
    
    -- Ruby level backtrace information ----------------------------------------
    

    下面还有好几屏幕

  • 在 20120804162920_devise_create_users.rb 里面看到没有:

    class DeviseCreateUsers < ActiveRecord::Migration
      def change
        create_table(:users) do |t|
          ## Database authenticatable
          t.string :email,              :null => false, :default => ""
          t.string :encrypted_password, :null => false, :default => ""
    
          ## Recoverable
          t.string   :reset_password_token
          t.datetime :reset_password_sent_at
    
          ## Rememberable
          t.datetime :remember_created_at
    
          ## Trackable
          t.integer  :sign_in_count, :default => 0
          t.datetime :current_sign_in_at
          t.datetime :last_sign_in_at
          t.string   :current_sign_in_ip
          t.string   :last_sign_in_ip
    
          ## Confirmable
          # t.string   :confirmation_token
          # t.datetime :confirmed_at
          # t.datetime :confirmation_sent_at
          # t.string   :unconfirmed_email # Only if using reconfirmable
    
          ## Lockable
          # t.integer  :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
          # t.string   :unlock_token # Only if unlock strategy is :email or :both
          # t.datetime :locked_at
    
          ## Token authenticatable
          # t.string :authentication_token
    
    
          t.timestamps
        end
    
        add_index :users, :email,                :unique => true
        add_index :users, :reset_password_token, :unique => true
        # add_index :users, :confirmation_token,   :unique => true
        # add_index :users, :unlock_token,         :unique => true
        # add_index :users, :authentication_token, :unique => true
      end
    end
    
    

    但是在 shema.rb 里面看到这些:

    ActiveRecord::Schema.define(:version => 20120804202523) do
    
      create_table "articles", :force => true do |t|
        t.string   "title"
        t.text     "content"
        t.datetime "created_at", :null => false
        t.datetime "updated_at", :null => false
      end
    
      create_table "roles", :force => true do |t|
        t.string   "name"
        t.integer  "resource_id"
        t.string   "resource_type"
        t.datetime "created_at",    :null => false
        t.datetime "updated_at",    :null => false
      end
    
      add_index "roles", ["name", "resource_type", "resource_id"], :name => "index_roles_on_name_and_resource_type_and_resource_id"
      add_index "roles", ["name"], :name => "index_roles_on_name"
    
      create_table "users", :force => true do |t|
        t.string   "email",                  :default => "", :null => false
        t.string   "encrypted_password",     :default => "", :null => false
        t.string   "reset_password_token"
        t.datetime "reset_password_sent_at"
        t.datetime "remember_created_at"
        t.integer  "sign_in_count",          :default => 0
        t.datetime "current_sign_in_at"
        t.datetime "last_sign_in_at"
        t.string   "current_sign_in_ip"
        t.string   "last_sign_in_ip"
        t.datetime "created_at",                             :null => false
        t.datetime "updated_at",                             :null => false
        t.string   "name"
        t.string   "role"
      end
    
      add_index "users", ["email"], :name => "index_users_on_email", :unique => true
      add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
    
      create_table "users_roles", :id => false, :force => true do |t|
        t.integer "user_id"
        t.integer "role_id"
      end
    
      add_index "users_roles", ["user_id", "role_id"], :name => "index_users_roles_on_user_id_and_role_id"
    
    end
    

    不太弄的明白

  • [NOTE]
    You may have encountered a bug in the Ruby interpreter or extension libraries.
    Bug reports are welcome.
    For details: http://www.ruby-lang.org/bugreport.html
    
    [1]    35754 abort      rails s
    
  • 现在重新编辑之前发布的 articles 的图片,也导致服务器退出这个问题

  • 楼主能否再扩展的写一下 在用 cancan 之前的配置? 比如 roles 这些?

    我是参照别的地方来添加 roles 的,然后套用你的代码,但是一直有问题, 现在是 user1 身份可以管理任何 action,但是其他身份的 user 没有权限,包括他自己先前发布的文章都不可以操作了。

  • 现在可以确定的是 我的 default 角色的 user 没有:manage 的权限,不知道哪里出错

  • #2 楼 @bony undefined method `user_id' for #Article:0x007f8ed88c8f48 哪个地方出错了?

  • 我用的 devise+cancan:

    #models/ability.rb

    class Ability
      include CanCan::Ability
    
      def initialize(user)
        user ||= User.new # guest user (not logged in)
    
        can :manage, :all if user.is? :admin
        can :manage, :all if user.is? :default
     end
    
    end
    

    #models/user.rb

    class User < ActiveRecord::Base
      rolify
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable
    
      attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :roles
    
      ROLES = %w[admin default banned]
    
      def is?(role)
        roles.include(tole.to_s)
      end
    
    
    end
    

    #controllers/articles_controller.rb

    class ArticlesController < ApplicationController
      load_and_authorize_resource :except => [:index, :show]
    
    end
    
    

    #db/migrate/20120804202523_add_role_to_users.rb

    class AddRoleToUsers < ActiveRecord::Migration
      def change
        add_column :users, :role, :string
      end
    end
    

    #db/schema.rb

    # encoding: UTF-8
    
    ActiveRecord::Schema.define(:version => 20120804202523) do
    
      create_table "roles", :force => true do |t|
        t.string   "name"
        t.integer  "resource_id"
        t.string   "resource_type"
        t.datetime "created_at",    :null => false
        t.datetime "updated_at",    :null => false
      end
    
      add_index "roles", ["name", "resource_type", "resource_id"], :name => "index_roles_on_name_and_resource_type_and_resource_id"
      add_index "roles", ["name"], :name => "index_roles_on_name"
    
      create_table "users", :force => true do |t|
        t.string   "email",                  :default => "", :null => false
        t.string   "encrypted_password",     :default => "", :null => false
        t.string   "reset_password_token"
        t.datetime "reset_password_sent_at"
        t.datetime "remember_created_at"
        t.integer  "sign_in_count",          :default => 0
        t.datetime "current_sign_in_at"
        t.datetime "last_sign_in_at"
        t.string   "current_sign_in_ip"
        t.string   "last_sign_in_ip"
        t.datetime "created_at",                             :null => false
        t.datetime "updated_at",                             :null => false
        t.string   "name"
        t.string   "role"
      end
    
      add_index "users", ["email"], :name => "index_users_on_email", :unique => true
      add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
    
      create_table "users_roles", :id => false, :force => true do |t|
        t.integer "user_id"
        t.integer "role_id"
      end
    
      add_index "users_roles", ["user_id", "role_id"], :name => "index_users_roles_on_user_id_and_role_id"
    
    end
    
    

    #views/articles/index.html.erb

    
    <ul class="clearfix">
      <% if can? :update, @article %>
      <li class="clearfix"><%= link_to 'Edit', edit_article_path(@article) %></li>
      <% end %>
      <% if can? :destroy, @article %>
      <li class="clearfix"><%= link_to 'Destroy', @article, method: :delete, data: {confirm: 'Are you sure?'} %></li>
      <% end %>
    </ul>
    
    
    

    #views/articles/show.html.erb

    <ul class="clearfix">
      <% if can? :update, @article %>
      <li class="clearfix"><%= link_to 'Edit', edit_article_path(@article) %></li>
      <% end %>
      <% if can? :destroy, @article %>
      <li class="clearfix"><%= link_to 'Destroy', @article, method: :delete, data: {confirm: 'Are you sure?'} %></li>
      <% end %>
    </ul>
    
    

    现在的情况是,除了 user1(估计是 admin) 拥有所有权限,其他身份的用户登陆之后都不能创建新的 article, 显示You are not authorized to access this page.,而且也无法访问了之前由他自己发布的 article 的 edit,destroy 功能,等于是完全变成浏览者的身份了。

  • 解决了, get 'dashboard' => 'dashboard#dashboard'

    我这样写就好了 谢谢

  • #1 楼 @Rei 谢谢 好久没见 呵呵,我找了一个 ruby 的实习工作了

  • #4 楼 @chucai #6 楼 @Rei 好的,今晚继续!我还是参照你的 code_campo 来。感觉就象做智商测验!发现规律!

  • #2 楼 @jhjguxin 好的谢谢!

  • 不知道是不是自己年级大了,记性不好了,另外我特别要求那种纯净的代码,没用的多一行都觉得是负担,所以根据教程用脚手架创建的有些地方就觉得是不是多余了

  • #3 楼 @huacnlee 好的 谢谢