`
dfinger
  • 浏览: 3442 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

读 Agile Web Development with Rails 笔记(二)

阅读更多
近日照例一步一步看下去。。
遇到第一个问题
用rake db:migrate的时候通不过。。。
我用网上下来的源程序依然通不过。。。

:decimal
#002_add_price.rb
class AddPrice < ActiveRecord::Migration
  def self.up
    add_column :products, :price, :decimal, :precision => 8, :scale =>
2, :default => 0
  end

  def self.down
    remove_column :products, :price
  end
end 


出错
引用

== CreateProducts: migrating ==================================================
-- create_table(:products)
   -> 0.0160s
== CreateProducts: migrated (0.0160s) =========================================

== AddPrice: migrating ========================================================
-- add_column(:products, :price, :decimal, {:precision=>8, :scale=>2, :default=>
0.0})
rake aborted!
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]

不知道什么问题。。。google之。在ror的讨论版里得知。。。
这个问题可能是因为decimal的支持问题。。。

改用edge版本是个方法。。。另外可以选择用sql语句倒入。。。
对于edge版本我是不了解。更没信心。于是还是sql导入把。。。

class AddPrice < ActiveRecord::Migration

  def self.up

    #add_column :products, :price, :decimal, :precision => 8, :scale => 2, :default => 0

    execute "ALTER TABLE products ADD COLUMN price DECIMAL(8,2) NOT NULL"  

  end


def self.down
remove_column :products, :price
end
end 


接下来看到显示价格问题.
希望把28.95显示为更为友好的$28.95...
$(*%U(@$*%
我显示成$0.28...
突然发现...原来第二版的英文书也不可靠...
最可靠的方法大概是花10几块美元买一pdf..可以随时更新的版本....
快被气坏了...

书中用
module StoreHelper
def format_price(amount)
dollars, cents = amount.divmod(100)
sprintf("$%d.%02d", dollars, cents)
end
end

在helper里面写了一个方法来解决这个问题.
并不难懂...
但是在最新的代码文件中....
他用了更简单的方法...
直接在view里面.也就是需要显示价格的页面中..
直接使用了.number_to_currency这个方法解决了一切问题...

也就是说..rails本身提供这个方法...如同java的某些工具类一般...

我必须要说...rails的官方文档可真不怎么友好...

不过依然足够好到可以找到我想找到的东西了...
[url]
http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000449[/url]
引用
number_to_currency(number, options = {})

Formats a number into a currency string. The options hash can be used to customize the format of the output. The number can contain a level of precision using the precision key; default is 2 The currency type can be set using the unit key; default is "$" The unit separator can be set using the separator key; default is "." The delimiter can be set using the delimiter key; default is "," Examples:

   number_to_currency(1234567890.50)     => $1,234,567,890.50
   number_to_currency(1234567890.506)    => $1,234,567,890.51
   number_to_currency(1234567890.50, {:unit => "&pound;", :separator => ",", :delimiter => ""}) => &pound;1234567890,50


rails的优点之一么?
避免重复造轮子?
分享到:
评论

相关推荐

    Agile+Web+Development+with+Rails中文版.pdf

    《Agile Web Development with Rails》是一本经典的Rails开发指南,中文版的出版使得更多的中国开发者能够深入理解并应用敏捷开发方法与Ruby on Rails框架。这本书是Rails开发者的必备参考资料,它详细介绍了如何...

    Agile Web Development with Rails 4

    Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...

    Agile Web Development with Rails

    《Agile Web Development with Rails》(敏捷Web开发:Ruby on Rails)这本书,作为Rails开发新手的教材,强调了敏捷开发方法,并以其帮助开发者建立起一个实用的Web应用。从给出的文件信息来看,这本书正在编写过程...

    Agile Web Development with Rails 1-14节_ppt(老师发的修正版)

    Agile Web Development with Rails 1-14节_ppt(老师发的修正版)

    agile web development with rails 5(英文电子书)

    agile web development with rails 5(英文电子书).............................................................................................................................................................

    Agile Web Development with Rails (PDF)

    《敏捷Web开发与Rails》是一本深度探讨如何利用Ruby on Rails框架进行敏捷Web开发的指导书籍,由Dave Thomas、David Heinemeier Hansson等多位在Rails社区有着深厚贡献的作者共同编写。本书不仅覆盖了Rails的基本...

    Agile Web Development with Rails 3nd Edition Beta.pdf

    ### 敏捷Web开发与Rails 3:关键知识点解析 #### 一、Rails版本与兼容性 本书《敏捷Web开发与Rails》第三版是基于Rails 2编写的。截至本书印刷时,当前可用的Rails Gem版本为2.1。书中所包含的所有代码均已在该...

    Agile Web Development with Rails 4th edition(敏捷Web开发与Rails:程序指南 第四版)

    《敏捷Web开发与Rails:程序指南 第四版》是一本深度探讨使用Ruby on Rails框架进行敏捷Web应用开发的专业书籍。本书旨在帮助开发者充分利用Rails 4的特性,提高开发效率,实现快速迭代和高质量的代码编写。 Rails是...

    Agile Web Development With Ruby On Rails第一及第二版

    《Agile Web Development With Ruby On Rails》是两本广受欢迎的书籍,主要涵盖了使用Ruby on Rails框架进行敏捷Web开发的知识。这本书的第1版和第2版分别详细讲解了如何运用敏捷开发方法来构建高效、可扩展且易于...

    Agile Web Development with Rails,Fourth Edition 第四版

    《敏捷Web开发与Rails》第四版是一本专为软件开发者设计的权威指南,全面涵盖了使用Ruby on Rails框架进行敏捷Web应用开发的知识。Rails 3是该版本的重点,它引入了许多新特性和改进,使得开发过程更为高效且灵活。...

    Pragmatic - Agile Web Development with Rails

    《Pragmatic - Agile Web Development with Rails》是Ruby on Rails框架的经典教材,旨在引导初学者高效地学习敏捷Web开发。这本书的第三版于2009年发布,它结合了Pragmatic Programmers的实用主义理念与Ruby on ...

    Agile Web Development with Rails, 2nd Edition

    Agile Web Development with Rails, 2nd Edition &lt;br&gt;有两份PDF文件,大小分别是7.39MB和6.55MB &lt;br&gt;作者: Dave Thomas , David Heinemeier Hansson , Leon Breedt , Mike Clark , James Duncan Davidson ,...

    Agile Web Development with Rails for Rails 3.2

    ### Agile Web Development with Rails for Rails 3.2 #### 核心知识点概览 - **Rails 3.2概述** - **敏捷开发方法论** - **Model-View-Controller (MVC) 模式** - **Ruby on Rails基础与高级特性** - **面向对象...

    Agile Web Development with Rails 4th(正式版).pdf

    Agile Web Development with Rails 4th(正式版).pdf

    Agile Web Development With Rails Fourth Edition

    ### Agile Web Development with Rails 第四版 #### 书籍概述与价值 《Agile Web Development with Rails》第四版是一本经典的Rails开发指南,旨在为开发者提供一套全面、实用且高效的敏捷开发方法论。本书由Sam ...

Global site tag (gtag.js) - Google Analytics