由于安装的是最新版Rails3.2+Ruby1.93,和书中当时的Rails 3.0.5不太一样,学习例子中遇到一些问题,总结如下:
1.Routing Error
No route matches [GET] "/assets/depot.css"
No route matches [GET] "/assets/logo.png"
Try running rake routes for more information on available routes.
原因:是由于Rails3.1以后Asset Pipeline默认是开着的,这样helper生产的连接不是以前public/javascripts而是/assets/javascripts/,同理/images和/stylesheets.
http://guides.rubyonrails.org/layouts_and_rendering.html#asset-tag-helpers
引用
If you are using Rails with the Asset Pipeline enabled, this helper will generate a link to /assets/javascripts/ rather than public/javascripts which was used in earlier versions of Rails. This link is then served by the Sprockets gem, which was introduced in Rails 3.1.
而这时assets目录在这三个目录任何一个下面即可:app/assets, lib/assets or vendor/assets
http://guides.rubyonrails.org/asset_pipeline.html
引用
Pipeline assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.
解决:rubyform上有人答复推荐降低rails版本到3.0.5,这个算是个快的解决办法,但总觉得没解决问题,回头用新的开发还是会遇到。
其实可以很简答解决问题:
在app/assets下的对应目录加入相应的css,js,pic即可
C:\Ruby193\work\depot\app\assets 的目录
2012/03/13 13:52 <DIR> .
2012/03/13 13:52 <DIR> ..
2012/03/13 13:35 <DIR> images
2012/03/13 09:58 <DIR> javascripts
2012/03/13 13:52 <DIR> stylesheets
2.Ajax无渐变效果(Highlighting Changes)
error:Missing template line_items/create, application/create
原因:3.1以后变化
解决:
1.\app\views\layouts\application.html.erb
<%= javascript_include_tag :defaults %> to <%= javascript_include_tag "application" %>
2.app/views/line_items/create.js.rjs 成create.js.erb,并修改内容:
page.replace_html('cart', render(@cart))
page[:current_item].visual_effect :highlight,
:startcolor => "#88ff88",
:endcolor => "#114411"
改成
$('#cart').html("<%= escape_javascript(render(@cart)) %>");
$('#current_item').css({ 'background-color': "#88ff88" }).animate({ 'background-color': "#114411" }, 1000); //在jquery中animate不支持background-color,所以看不到变回去的动画效果,如果要实现得加载jquery.color.js,详情见官网api
3.隐藏提交订单后的提示信息(Iteration G1: Capturing an Order)
app/views/line_items/create.js.erb
page.select("#notice").each { |notice| notice.hide}
改成
$('#notice').hide();
4.well-paginate出错(Iteration G3: Pagination)
error:
NoMethodError: undefined method `paginate' for
原因:得
安装well-paginate,安装完后重启rails server才行
按官网:(
https://github.com/mislav/will_paginate/wiki/Installation)
在depot\Gemfile添加:
## Gemfile for Rails 3, Sinatra, or Merb
gem 'will_paginate'
然后:
bundle install
接着:
重启rails server