Rails Tip: 檔案名稱不要有數字

紅寶鐵軌客
Join to follow...
Follow/Unfollow Writer: 紅寶鐵軌客
By following, you’ll receive notifications when this author publishes new articles.
Don't wait! Sign up to follow this writer.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.
寫程式中、折磨中、享受中 ......
728   0  
·
2018/05/25
·
2 mins read


我剛踩到一個很大的雷,害我多花了快兩個小時,趕快記起來,幾本上,就是:

避免檔案名稱中有數字!

我喜歡用 generate scaffold,雖然很多情況下都不一定要會用到 views,但是,總覺得有一天可能要用,今天剛好要新增一個功能,要用到幾個新的資料檔,我就跟以前一樣,呼叫 scaffold

rails g scaffold ip_2_city ip_cidr:inet:index city_id:string

夠簡單吧,一個 table 就兩個欄位,執行都很順利,DB migrate 都很順利,我也很快的把 controller 跟 views 寫好了,但是一個簡單的執行 view index,馬上就掛了:

NameError: uninitialized constant Ip2City

真是很奇怪,完全不知道錯在那裡,用 rails c 確認一下,結果一執行:Ip2City.all 一樣掛,一樣 NameError: uninitialized constant,這就有趣了,我東試西試,哈!原來跟 model 的檔案名稱有關,我將 model 的 ip_2_city.rb 改成 ip_city.rb 就不會 name error 了(這一改,當然會有其他錯誤),但,這⋯⋯ 為什麼為什麼啊?

又花了好久,真找不出來問題,算了,直接退回原點:

rake db:rollbacke

rails destroy scaffold ip_city

重新換個名,就叫 IpCity 好了,一樣呼叫 scaffold,所有都沒改,就這樣,所有的問題都沒了,我很無言啊,我想,也許完全照 Rails 手冊用 Ip2City 也許就可以,用底線的寫法本來就不對,但是我已經用了好幾次都很好啊,結論,這就是 rails,這就是 Web,總有些稀奇跟古怪,這才有趣不是麼?!


WriterShelf™ is a unique multiple pen name blogging and forum platform. Protect relationships and your privacy. Take your writing in new directions. ** Join WriterShelf**
WriterShelf™ is an open writing platform. The views, information and opinions in this article are those of the author.


Article info

分類於:
標籤:
合計:388字


Share this article:
About the Author

很久以前就是個「寫程式的」,其實,什麼程式都不熟⋯⋯
就,這會一點點,那會一點點⋯⋯




Join the discussion now!
Don't wait! Sign up to join the discussion.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.