第二版針對的是rails1.2版本,而我入手已經是2.2了,差別很大,這裏記下我所遇到的問題。
1、數據庫連接。
書中例子Depot使用的MySQL版本爲5.0,而我最初使用的是5.1版本,始終無法正常連接,後來嘗試Postgresql8.3,再次失敗,最後卸載MySQL5.1,安裝5.0版本搞定。
2、scaffold在rails2.2中的問題
查得scaffold在Rails2.0時就不再提供,而需要通過插件install,但是我install後繼續有問題。參考了http://jkfzero.iteye.com/blog/284774中的內容,看來動態使用暫時搞不定,於是用文章中提到的辦法,靜態使用scaffold來創建model,control and helper,要注意的是,之前的東西要清理乾淨,包括表格,helper,不然使用時還是會有問題,最後還必須執行一次rake db:migrate,不然在new操作時是會報錯的。針對這個問題,國外的論壇上是建議將rails的版本回退到1.2.6,如果再遇到類似的問題,我只好這樣做了-_-#.
ps: 回退rails版本的方法,執行gem uninstall rails卸載,執行gem install rails --version 1.2.6安裝特定版本。
3、添加price字段
按照書中的操作可以改變db中表格結構,但是瀏覽器刷新後,貌似scaffold並沒有被重新執行,畢竟是靜態的嘛,所以要修改scaffold生成的代碼,瀏覽了下control,model,views,發現views和表結構相關,修改所有的erb文件,依葫蘆畫瓢就可以了:)
4、迭代A4中有涉及靜態scaffold和動態scaffold的內容
5、optimize list view的html有不支持的地方,下面這段有問題,先掠過:
<%= if @product_pages.current.previous
link_to("Previous page", { :page => @product_pages.current.previous })
end
%>
<%= if @product_pages.current.next
link_to("Next page", { :page => @product_pages.current.next })
end
%>
6、destroy 鏈接構造不正確。
書上的代碼是
<%= link_to 'Destroy', { :action => 'destroy', :id => product },
:confirm => "Are you sure?",
:method => :post %>
構造出來類似"http://127.0.0.1:3000/products/9",正確的應該形似"http://127.0.0.1:3000/products/9/destroy",查閱了API guide,正確的寫法是:
<%= link_to 'Destroy', {:action => 'destroy', :id => product },
:confirm => "Are you sure?",
:method => :delete %>
7、創建購物車的例子cart,會有異常ActionController::InvalidAuthenticityToken,因爲2.0+的版本的新加控制( ensuring that all forms are coming from the current web application, not a forged link from another site),見http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html,我們需要在enviroment.rb中新加一條config.action_controller.allow_forgery_protection = false去disable它。
8、Ajax初始那個例子,如下,要去掉等號才正確,不然有compile error
<% form_remote_tag :url=> { :action=>:add_to_cart,:id=>product } do %>
<%= submit_tag "Add to cart" %>
<% end %>
9、Ajax初體驗中,FF3不需要後續調整就能有ajax的效果了,而IE還是需要加上諸如以下的附加代碼以及rjs
<%= javascript_include_tag :defaults %>
10、回退db可以執行 rake db:migrate VERSION=20081212103700類似的cli,複雜點的話還要配以刪除table的方式。
11、TODO